JComponents disappearing after calling mouseClicked()

喜夏-厌秋 提交于 2019-11-26 17:29:17

问题


I am writing a Java GUI program with Swing. The interface looks like this:

When the user clicks on one of the pictures on the right hand side, I want a small preview of it to show up in the orange area in the upper left corner. I pull all of the image files from a directory on my computer via a SwingWorker thread. In the SwingWorker's done() method, I add a mouseClicked listener to each object (a TaggableImage object from a class I created. It has a BufferedImage and extends JComponent, overriding the paintComponent() method). Within the mouseClicked listener, I call the orange panel's add() method and pass it the TaggableImage.

My Question:

When I click on one of the images, it does not get added to the orange panel, and it disappears from the panel on the right side. If I click all of the images, I will be left with a blank right panel. Why does this happen?

Here is my entire SwingWorker thread:

SwingWorker<ArrayList<TaggableImage>, Void> loadImagesAsyncWorker = new SwingWorker<ArrayList<TaggableImage>, Void>() {

    ArrayList<TaggableImage> img_list = new ArrayList<TaggableImage>();

    @Override
    protected ArrayList<TaggableImage> doInBackground() throws Exception {
        File currentDirectory = new File(".");
        getFileInDirectory(currentDirectory);

        return null;
    }

    // recursively get all files in this directory and child directories
    private void getFileInDirectory(File cd) {

        File[] file_list = cd.listFiles();
        for (File f : file_list) {

            if (f.isDirectory()) {
                getFileInDirectory(f);

            } else if (f.isFile()) {
                BufferedImage bf = null;

                String extension = "";

                int i = f.getName().lastIndexOf('.');
                int p = Math.max(f.getName().lastIndexOf('/'), f.getName()
                        .lastIndexOf('\\'));

                if (i > p) {
                    extension = f.getName().substring(i + 1);
                }
                System.out.println(f.getName());

                if (extension.equals("jpg")) {
                    try {
                        bf = ImageIO.read(f);

                        TaggableImage ti = new TaggableImage(bf);

                        img_list.add(ti);

                    } catch (IOException e) {

                    }
                }
            }

        }
    }

    public void done() {

        for (TaggableImage ti : img_list) {

            final TaggableImage final_ti = ti;

            ti.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    final_ti.setMaximumSize(new Dimension(30, 30));

                    BorderLayout layout = (BorderLayout) getContentPane()
                            .getLayout();
                    if (layout.getLayoutComponent(BorderLayout.CENTER) != null) {
                        img_prev_panel.remove(layout
                                .getLayoutComponent(BorderLayout.CENTER));

                    }

                    //img_prev_panel = the orange panel
                    img_prev_panel.add(final_ti, BorderLayout.CENTER);
            });

        }
    }

};

And here is my TaggableImage class:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import javax.swing.JComponent;

public class TaggableImage extends JComponent {
    private static final long serialVersionUID = 1L;
    private BufferedImage bufferedImage;
    private int height;
    private int width;

    public TaggableImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

    public TaggableImage(BufferedImage bufferedImage, int width, int height) {
        this.bufferedImage = bufferedImage;
        this.width = width;
        this.height = height;

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bufferedImage, 0, 0, null);
    }
}

回答1:


Consider using a JList for the right panel to take advantage of the flexible layout options and selection handling, as shown here and below.

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 {

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

    private ListPanel listPanel = new ListPanel();
    private DisplayPanel displayPanel = new DisplayPanel();

    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(SIZE, SIZE);
        }
    }

    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()) {
                    displayPanel.label.setText((String) dlm.getElementAt(e.getLastIndex()));
                }
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JSplitPane jsp = new JSplitPane();
        jsp.setRightComponent(new JScrollPane(listPanel));
        jsp.setLeftComponent(displayPanel);
        f.add(jsp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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


来源:https://stackoverflow.com/questions/25167235/jcomponents-disappearing-after-calling-mouseclicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!