-2

I need to show images and images numbers are unknown - maybe 5 maybe 10 maybe more and I have no idea how I should display them.

This is my code and display only one image.

public class NewClass extends JFrame {

    public static void main(String []args) throws IOException{
        BufferedImage img=ImageIO.read(new File("D:\\A-programmer-Life.jpg"));
        ImageIcon icon=new ImageIcon(img);
        JFrame frame=new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setSize(500,500);
        JLabel lbl=new JLabel();
        lbl.setIcon(icon);
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ImPHL1
  • 31
  • 6

1 Answers1

1

Display the images in a JList. The list will allow you to control the number of rows of images and then you add the list to a JScrollPane so scrollbars can appear if necessary.

You can add an Icon to the list and it will render as an image.

Check out the section from the Swing tutorial on How to Use Lists for more information.

The other option is to use a panel with a GridLayout. The labels will wrap to a new row when the first row is full. Again you would add this panel to a JScrollPane. The tutorial also has an example of using a GridLayout.

camickr
  • 321,443
  • 19
  • 166
  • 288