0

I saw a piece of code on this website https://stackoverflow.com/a/25170471/230513 which creates a panel and splits the screen into 2 halves.

What I want is for the user to click on an icon and then get confirmation, which then gets put onto Panel 1. The user should then pick another icon and that should go to panel 2. At the moment, if the user clicks on one icon it displays on both panels.

This is the code;

package GUI;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @see https://stackoverflow.com/a/25170471/230513
 */
public class ListDisplayPanel extends Component{
    private JPanel contentPane;

    private static final Icon icon = UIManager.getIcon("html.pendingImage");

    private ListPanel listPanel = new ListPanel();
    private DisplayPanel displayPanel1 = new DisplayPanel();
    private DisplayPanel displayPanel2 = new DisplayPanel();
    private JPanel dpHold = new JPanel();

    private class DisplayPanel extends JPanel {

        private static final int SIZE = 256;
        private JLabel label = new JLabel();

        public DisplayPanel() {
            this.add(label);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D temp = (Graphics2D) img.getGraphics();
            icon.paintIcon(this, temp, 0, 0);
            temp.dispose();
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }
    }

    private class ListPanel extends JPanel {

        private static final int N = 5;
        private DefaultListModel dlm = new DefaultListModel();
        private JList list = new JList(dlm);

        public ListPanel() {
            super(new GridLayout());
            for (int i = 0; i < N * N; i++) {
                String name = "Cell-" + String.format("%02d", i);
                dlm.addElement(name);
            }
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
            list.setVisibleRowCount(N);
            list.setCellRenderer(new ListRenderer());
            list.addListSelectionListener(new SelectionHandler());
            this.add(list);
        }

        private class ListRenderer extends DefaultListCellRenderer {

            @Override
            public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected, boolean cellHasFocus) {
                JLabel label = (JLabel) super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
                label.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
                label.setIcon(icon);
                label.setHorizontalTextPosition(JLabel.CENTER);
                label.setVerticalTextPosition(JLabel.BOTTOM);
                return label;
            }
        }

        private class SelectionHandler implements ListSelectionListener {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    displayPanel1.label.setText((String) dlm.getElementAt(e.getLastIndex()));
                    displayPanel2.label.setText((String) dlm.getElementAt(e.getLastIndex()));
                }
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("Generate");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JSplitPane jsp = new JSplitPane();
        jsp.setLeftComponent(new JScrollPane(listPanel));
        JSplitPane jsp2 = new JSplitPane();
        jsp2.setTopComponent(displayPanel1);
        jsp2.setBottomComponent(displayPanel2);
        jsp.setRightComponent(jsp2);
        f.add(jsp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        /*EventQueue.invokeLater(() ->*/ {
            new ListDisplayPanel().display();
        };
    }
}
Community
  • 1
  • 1

1 Answers1

1

The ListSelectionEvent of the ListSelectionListener set the first index and the last index in an interesting way. I'm using Java 7.

If you selected an element that was less than the previously selected element, the ListSelectionEvent would update the first index.

If you selected an element that was greater than the previously selected element, the ListSelectionEvent would update the last index.

This happened even when single selection was specified on the JList.

The other change I made to the ListSelectionListener was to add a toggle int so that the selected text would go to panel 1 first, then panel 2, then back to panel 1.

I made a few other changes which eliminated the compiler warnings.

package com.ggl.testing;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @see http://stackoverflow.com/a/25170471/230513
 */
public class ListDisplayPanel extends Component {

    private static final long serialVersionUID = 6728567333943887514L;

    private static final Icon icon = UIManager.getIcon("StockMarket.png");

    private ListPanel listPanel = new ListPanel();
    private DisplayPanel displayPanel1 = new DisplayPanel();
    private DisplayPanel displayPanel2 = new DisplayPanel();

    private class DisplayPanel extends JPanel {

        private static final long serialVersionUID = 7705513454136519778L;

        private JLabel label = new JLabel();

        public DisplayPanel() {
            this.add(label);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (icon == null)
                return;

            BufferedImage img = new BufferedImage(icon.getIconWidth(),
                    icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D temp = (Graphics2D) img.getGraphics();
            icon.paintIcon(this, temp, 0, 0);
            temp.dispose();
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 200);
        }
    }

    private class ListPanel extends JPanel {

        private static final long serialVersionUID = -6104547005775751025L;

        private static final int N = 5;
        private DefaultListModel<String> dlm = new DefaultListModel<String>();
        private JList<String> list = new JList<String>();

        public ListPanel() {
            super(new GridLayout());
            for (int i = 0; i < N * N; i++) {
                String name = "Cell-" + String.format("%02d", i);
                dlm.addElement(name);
            }
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
            list.setVisibleRowCount(N);
            list.setCellRenderer(new ListRenderer());
            list.addListSelectionListener(new SelectionHandler());
            list.setModel(dlm);

            this.add(list);
        }

        private class ListRenderer extends DefaultListCellRenderer {

            private static final long serialVersionUID = 1863262304255858334L;

            @SuppressWarnings("rawtypes")
            @Override
            public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                JLabel label = (JLabel) super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
                label.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
                label.setIcon(icon);
                label.setHorizontalTextPosition(JLabel.CENTER);
                label.setVerticalTextPosition(JLabel.BOTTOM);
                return label;
            }
        }

        private class SelectionHandler implements ListSelectionListener {

            private int previousIndex = -1;
            private int whichPanel = 1;

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    int first = e.getFirstIndex();
                    int last = e.getLastIndex();

                    if (first == last) {
                        previousIndex = first;
                    } else if (first == previousIndex) {
                        previousIndex = last;
                    } else {
                        previousIndex = first;
                    }

                    if (whichPanel == 1) {
                        displayPanel1.label.setText(dlm.get(previousIndex));
                        whichPanel = 2;
                    } else if (whichPanel == 2) {
                        displayPanel2.label.setText(dlm.get(previousIndex));
                        whichPanel = 1;
                    }
                }
            }

        }
    }

    private void display() {
        JFrame f = new JFrame("Generate");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JSplitPane jsp = new JSplitPane();
        jsp.setLeftComponent(new JScrollPane(listPanel));
        JSplitPane jsp2 = new JSplitPane();
        jsp2.setTopComponent(displayPanel1);
        jsp2.setBottomComponent(displayPanel2);
        jsp.setRightComponent(jsp2);
        f.add(jsp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ListDisplayPanel().display();
            }
        });
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111