Adding multiple images in single JPanel

后端 未结 2 1249
情话喂你
情话喂你 2020-12-06 14:40

I have a JPanel I want to add the multiple images in that panel & also offer a vertical scroll to show the images. The images might be of different sizes.<

2条回答
  •  悲&欢浪女
    2020-12-06 15:33

    For unequally sized images, use a text &/or 'small icon' renderer in a JList, & put it in the PAGE_START of a BorderLayout. Add a listener to it, then display the selected image in a JLabel in a JScrollPane in the CENTER. Something like this:

    ImageList

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import javax.imageio.ImageIO;
    
    public class ImageList {
    
        private JPanel gui;
        private JFileChooser fileChooser;
        FilenameFilter fileNameFilter;
        private JMenuBar menuBar;
        DefaultListModel model; 
    
        ImageList() {
            gui = new JPanel(new GridLayout());
    
            JPanel imageViewContainer = new JPanel(new GridBagLayout());
            final JLabel imageView = new JLabel();
            imageViewContainer.add(imageView);
    
            model = new DefaultListModel(); 
            final JList imageList = new JList(model);
            imageList.setCellRenderer(new IconCellRenderer());
            ListSelectionListener listener = new ListSelectionListener() {
    
                @Override
                public void valueChanged(ListSelectionEvent lse) {
                    Object o = imageList.getSelectedValue();
                    if (o instanceof BufferedImage) {
                        imageView.setIcon(new ImageIcon((BufferedImage)o));
                    }
                }
    
            };
            imageList.addListSelectionListener(listener);
    
            fileChooser = new JFileChooser();
            String[] imageTypes = ImageIO.getReaderFileSuffixes();
            FileNameExtensionFilter fnf = new FileNameExtensionFilter("Images", imageTypes);
            fileChooser.setFileFilter(fnf);
            File userHome = new File(System.getProperty("user.home"));
            fileChooser.setSelectedFile(userHome);
    
    
    
            fileNameFilter = new FilenameFilter() {
                @Override 
                public boolean accept(File file, String name) {
                    return true;
                }
            };
    
            menuBar = new JMenuBar();
            JMenu menu = new JMenu("File");
            menuBar.add(menu);
            JMenuItem browse = new JMenuItem("Browse");
            menu.add(browse);
            browse.addActionListener(new ActionListener(){
                @Override 
                public void actionPerformed(ActionEvent ae) {
                    int result = fileChooser.showOpenDialog(gui);
                    if (result==JFileChooser.APPROVE_OPTION) {
                        File eg = fileChooser.getSelectedFile();
                        // this will be an image, we want the parent directory
                        File dir = eg.getParentFile();
                        try {
                            loadImages(dir);
                        } catch(Exception e) {
                            e.printStackTrace();
                            JOptionPane.showMessageDialog(
                                    gui, 
                                    e, 
                                    "Load failure!", 
                                    JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            });
    
            gui.add(new JSplitPane(
                    JSplitPane.HORIZONTAL_SPLIT, 
                    new JScrollPane(
                            imageList, 
                            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), 
                    new JScrollPane(imageViewContainer)));
        }
    
        public void loadImages(File directory) throws IOException {
            File[] imageFiles = directory.listFiles(fileNameFilter);
            BufferedImage[] images = new BufferedImage[imageFiles.length];
            model.removeAllElements();
            for (int ii=0; ii

    If the images are all the same size, see this answer. (This was at the top of the answer before the spec. was clarified. I left it since it is such a cute screen-shot.)

提交回复
热议问题