How can i display multiple Pictures in a java.awt.Container best?

hi,

I just started with a new Project, it works just like a FileManager.

On entering a directory all the files in it shall be displayed in a scrollable area. Displayed means here a Graphical representation of the file, in case of a picture a small preview version of it.

I put together a javax.swing.JScrollPane with a java.awt.Container and viewport. The Container has a FlowLayout.

Now for every image in a Directory I create a Component-class in witch I overwrote the public void paint(Graphics g) to display a scaled version of the image, this scaled version is created in the Constructor of my Component-class.

this works fine for just a few images, but if i try to open a directory with a lot of pictures it takes a long time at the beginning and sometimes it just kills the application.

so i changed in a way that only when a Component is in view of the visible rectangle of the scroll pane the scaled image will be created, and when the component gets out of view the image is set to null.

now it is running without killing the application, but it takes way too long to scroll now, because the images are loaded all the time.

so i figured i must be doing something wrong, so what is the best way to get small previews for up to a few hundred files and then display them in a scrollable area? (like in Windows Explorer if you activate the miniature preview)

if anyone can give me some hints, that would be great

thanks shirasuresh

[1504 byte] By [Shirasuresha] at [2007-11-27 9:41:01]
# 1

This might give you some ideas:

import java.awt.*;

import javax.swing.*;

public abstract class LazyIcon implements Icon {

private int width;

private int height;

private Image image;

public LazyIcon(int width, int height) {

this.width = width;

this.height = height;

}

protected abstract Image createImage();

public int getIconHeight() {

return width;

}

public int getIconWidth() {

return height;

}

public void paintIcon(Component comp, Graphics g,int x, int y) {

if (image == null)

image = createImage();

g.drawImage(image, x, y, comp);

}

}

You could also do this with a custom component class, but I like the JLabel + Icon combination.

Any if you want to get fancy, you could load images in another thread, at low priority, so when the user is just sitting there, your code is preparing the panel for scrolling ;-)

Message was edited by:

BigDaddyLoveHandles

BigDaddyLoveHandlesa at 2007-7-12 23:19:07 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks for the fast answer.I try it out right now.
Shirasuresha at 2007-7-12 23:19:07 > top of Java-index,Java Essentials,Java Programming...