I’m trying to create a panel with a paginated list of thumbnails and their filenames. The list also needs to be sortable by various metadata attached with the file. The thumbnails are 120px wide by 90px high. So far I have:
public void PhotoCatalog extends JPanel {
private transient SortedList<PhotoMetadata> sortedThumbList;
public void PhotoCatalog() {
setLayout(new GridLayout(sortedThumbList % 3, 3));
Iterator<PhotoMetadata> iterator = sortedThumbList.iterator();
while (iterator.hasNext()) {
Thumbnail thumbnail = new Thumbnail(iterator.next());
JPanel panel = new JPanel(new BorderLayout());
panel.add(thumbnail, BorderLayout.NORTH);
panel.add(new JLabel(iterator.next().getFilename(), BorderLayout.SOUTH);
this.add(panel);
}
}
public class Thumbnail extends JPanel {
BufferedImage thumbnail = null;
public void Thumbnail(PhotoMetadata data) {
try {
thumbnail = ImageIO.read(new File(data.getFilename()));
}
catch (IOException e) {}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(thumbnail, 0, 0, null);
}
}
I need to intergrate the actual list to be able to sort and paginate but I’m stumped where to begin and which list to use. The code I have so far displays a black image that isn’t the right size.
1) How do I get the image to display in the catalog at the correct size?
2) How do I integrate a sortable list to add the thumbnails and their metadata to?
Thanks!
Your call to
drawImage()can have a width and height that will scale the image. You can add eachthumbnailto aJListin sorted order. AJListlets you change how the pictures get wrapped. Put the metadata in a tooltip for eachthumbnail.Edit:
Yes, add the list to a
JScrollPane.Yes; you’ll probably want to keep your
ListModelsorted by overriding the add/insert methods ofDefaultListModel.