Leniaal
Leniaal

Reputation: 1797

Displaying a grid of images

I'm looking for a way to load images into my application and then displaying them in a grid. Kind of like google image search only the thumbnails will all be in the same size. The amount of images will be random, so it has to be able to scroll. Also I have to be able to interact with the images, so when I click it I have to be able to fire an event.

What would be the best way to create this kind of a grid? If so, a link to a tutorial would be helpful!

Thanks in advance

Upvotes: 0

Views: 6775

Answers (2)

pedromss
pedromss

Reputation: 2453

Since you want to interact with the images, I would say drawing them is out of the question, so my suggestion would be to create JLabels to hold the images and simply add the JLabels to a panel with a gridLayout.

As for interacting add a listener (mouse probably) to each JLabel, and it will easily detect wich one you clicked on.

Example:

JLabel j1 = new JLabel();
j1.setIcon(new ImageIcon("path to your image");

j1.addMouseListener(...);

JPanel jp = new JPanel(new GridLayout(nRows, nCols);
Jp.add(j1);  

Upvotes: 3

camickr
camickr

Reputation: 324207

Just a JList with either horizontal or vertical wrapping. You just add an ImageIcon to the model and the image will be rendered properly. Read the section from the Swing tutorial on How to Use Lists for more information. A JList already provides "selection" type features.

Upvotes: 0

Related Questions