Search code examples
javaimagejpanelgrid-layoutjapplet

How to display images using GridLayout on JPanel?


What would be the most appropriate way to display 4 images using a GridLayout(2,2) on a JPanel?

Problem solved!! This is how I did it. It may not be efficient, but it's easy to read and it works :) Feel free to let me know how this can be improved! I'm always looking for ways to improve how I code!

//      Create panel and set layout
        pFlag= new JPanel();
        pFlag.setLayout(new GridLayout(2,2,10,10)); 

//      Get image       
        flag1Img = getImage(getCodeBase(), "croatia.png");
        flag2Img = getImage(getCodeBase(), "eng.png");
        flag3Img = getImage(getCodeBase(), "romania.png");
        flag4Img = getImage(getCodeBase(), "spain.png");        

//      Set as icon     
        flag1Icon = new ImageIcon(flag1Img);
        flag2Icon = new ImageIcon(flag2Img);
        flag3Icon = new ImageIcon(flag3Img);
        flag4Icon = new ImageIcon(flag4Img);

//      Create JLabel       
        flag1Label = new JLabel();
        flag2Label = new JLabel();
        flag3Label = new JLabel();
        flag4Label = new JLabel();

//      Set JLabel alignment        
        flag1Label.setHorizontalAlignment(JLabel.CENTER);
        flag1Label.setVerticalAlignment(JLabel.CENTER);
        flag2Label.setHorizontalAlignment(JLabel.CENTER);
        flag2Label.setVerticalAlignment(JLabel.CENTER);
        flag3Label.setHorizontalAlignment(JLabel.CENTER);
        flag3Label.setVerticalAlignment(JLabel.CENTER);
        flag4Label.setHorizontalAlignment(JLabel.CENTER);
        flag4Label.setVerticalAlignment(JLabel.CENTER);     

//      Set JLabels as icons
        flag1Label.setIcon(flag1Icon);
        flag2Label.setIcon(flag2Icon);
        flag3Label.setIcon(flag3Icon);
        flag4Label.setIcon(flag4Icon);

//      Assign icons to images
        pFlag.add(flag1Label);
        pFlag.add(flag2Label);
        pFlag.add(flag3Label);
        pFlag.add(flag4Label);

        con.add(pFlag);

Solution

  • Just put your images into JLabels.

    JFrame frame = new JFrame("Test");
    JPanel panel = new JPanel(new GridLayout(2, 2));
    frame.setContentPane(panel);
    
    frame.setVisible(true);
    JLabel label1 = new JLabel();
    panel.add(label1);
    JLabel label2 = new JLabel();
    panel.add(label2);
    JLabel label3 = new JLabel();
    panel.add(label3);
    JLabel label4 = new JLabel();
    panel.add(label4);
    
    try {
        BufferedImage myPicture = ImageIO.read(new File("test.jpg"));
    
        label1.setIcon(new ImageIcon(myPicture));
        label2.setIcon(new ImageIcon(myPicture));
        label3.setIcon(new ImageIcon(myPicture));
        label4.setIcon(new ImageIcon(myPicture));
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    frame.pack();
    frame.setMinimumSize(frame.getPreferredSize());
    

    UPDATE updated as Andrew Thompson suggested

    This is a very simple Applet displays 4 images from an URL.

    public class Main extends JApplet {
    
        public void paint(Graphics g) {
            JPanel panel = new JPanel(new GridLayout(2, 2));
            add(panel);
    
            JLabel label1 = new JLabel();
            panel.add(label1);
            JLabel label2 = new JLabel();
            panel.add(label2);
            JLabel label3 = new JLabel();
            panel.add(label3);
            JLabel label4 = new JLabel();
            panel.add(label4);
    
            try {
                URL url = new URL("YOU_IMAGE_URL.jpg");
                Image myPicture = getImage(url);
    
                label1.setIcon(new ImageIcon(myPicture));
                label2.setIcon(new ImageIcon(myPicture));
                label3.setIcon(new ImageIcon(myPicture));
                label4.setIcon(new ImageIcon(myPicture));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }