How to display multiple images

J

John Broadley

I am building a simple Java application. I have a list box in which I
list a list of image files .jpg, and I want to create an area beside
the listbox, where whenever the user click on an image in the list, I
can display the image for him. I tried the ImageIcon class, and
creating an instance and putting it in a JLabel, which is in a
scrollable window. It works fine. But now, when I want to display a
new image. Do I need to create all these objects again and agian. What
if I have 100 images, will I end up with a huge number of objects
creates my my program. Please note that the average size of the images
is 500kb.

I saw also a lot of talking about Toolkit related code, but I was not
able to use such library in my Eclipse platform. Do I need to import
anything to be able to use the Toolkit facilities like getting for
example the screen resolution?


Appreciate your help.
 
P

Paul Lutus

John said:
I am building a simple Java application. I have a list box in which I
list a list of image files .jpg, and I want to create an area beside
the listbox, where whenever the user click on an image in the list, I
can display the image for him. I tried the ImageIcon class, and
creating an instance and putting it in a JLabel, which is in a
scrollable window. It works fine. But now, when I want to display a
new image. Do I need to create all these objects again and agian.

What objects are those? Be specific.
What
if I have 100 images, will I end up with a huge number of objects
creates my my program.

No, only one image is loaded at a time, if you design your program
appropriately.
Please note that the average size of the images
is 500kb.

My digital camera creates 3072x2048 size images, and when displayed in true
color (3 bytes per pixel), that is over 18 megabytes per image when
uncompressed and loaded into memory. But in my slideshow Java program I
only have one image in memory at a time, so this is no problem.
I saw also a lot of talking about Toolkit related code, but I was not
able to use such library in my Eclipse platform. Do I need to import
anything to be able to use the Toolkit facilities like getting for
example the screen resolution?

When your user clicks a thumbnail, load the appropriate image. Arrange for
this action to abandon the prior image, and the garbage collector will
reclaim its memory.

Do you want more specific information? Post more specific information. Post
your code.
 
A

Andrew Thompson

I am building a simple Java application. I have a list box in which I
list a list of image files .jpg, ...

A better group for GUI related matters is described here..
I saw also a lot of talking about Toolkit related code,

Make sure you steer clear of
Toolkit.getImage() which states..
"Since the mechanism required to facilitate this sharing
of Image objects may continue to hold onto images that
are no longer of use for an indefinite period of time,
developers are encouraged to implement their own caching
of images by using the createImage variant wherever available. "

Caching is not good when you have a slideshow
of 500Kb+ images.
...but I was not
able to use such library in my Eclipse platform.

Eclipse is neither the problem nor the solution
in this one. The problem is in your code.
..Do I need to import
anything to be able to use the Toolkit facilities

Toolkit tk = Tookmit.getDefaultToolkit();
Appreciate your help.

You're welcome.
 
A

ak

I am building a simple Java application. I have a list box in which I
list a list of image files .jpg, and I want to create an area beside
the listbox, where whenever the user click on an image in the list, I
can display the image for him. I tried the ImageIcon class, and
creating an instance and putting it in a JLabel, which is in a
scrollable window. It works fine. But now, when I want to display a
new image. Do I need to create all these objects again and agian.
no just call
Image image = Toolkit.getDefaultToolkit.createImage();
ImageIcon#setImage(image);
 
F

FISH

I am building a simple Java application. I have a list box in which I
list a list of image files .jpg, and I want to create an area beside
the listbox, where whenever the user click on an image in the list, I
can display the image for him. I tried the ImageIcon class, and
creating an instance and putting it in a JLabel, which is in a
scrollable window. It works fine. But now, when I want to display a
new image. Do I need to create all these objects again and agian. What
if I have 100 images, will I end up with a huge number of objects
creates my my program. Please note that the average size of the images
is 500kb.

Possibly the cleanest solution (and the one with potentially the least
resource overhead) is to make your own Icon implementation, which
supports 'hot swapping' of images. The Icon interface is very straight
forward, and you shouldn't have any problem rolling your own. As well
as the Icon methods, add your own method which allows for the current
image to change. Don't forget to inform the icon's container that it
needs revalidating.

I saw also a lot of talking about Toolkit related code, but I was not
able to use such library in my Eclipse platform. Do I need to import
anything to be able to use the Toolkit facilities like getting for
example the screen resolution?

java.awt.Toolkit ...? :)


-FISH- ><>
 
A

ak

Possibly the cleanest solution (and the one with potentially the least
resource overhead) is to make your own Icon implementation, which
supports 'hot swapping' of images
is it not already implemented in ImageIcon?
 
F

FISH

is it not already implemented in ImageIcon?


You're right! ImageIcon has a setImage() method, which I didn't even
know existed. For some strange reason I'd got it into my head that
ImageIcon's were immutable once created. Thanks for correcting me.


-FISH- ><>
 
Joined
Nov 8, 2008
Messages
1
Reaction score
0
same problem

Dear John,

I wanna to do the same stuff as you've done
list a list of image files .jpg, and I want to create an area beside
the listbox, where whenever the user click on an image in the list, I
can display the image for him. I tried the ImageIcon class, and
creating an instance and putting it in a JLabel, which is in a
scrollable window.​
Unfortunately Ii doesn't work!! Just for the first image the image shown in scrollpane, but after clicking in other image number, the image does not appear.

My code is this:

PlanarImage im=JAI.create("fileload", path);
//decreasing size of image
BufferedImage image = new BufferedImage((int) Math.round(512*scale),(int) Math.round(512*scale), BufferedImage.TYPE_INT_RGB);

image.createGraphics().drawImage(im.getAsBufferedImage(), 0, 0,image.getWidth(), image.getHeight(), null);

ImageIO.write(image, "PNG", new File(path));

images = new ImageIcon(path);
lable = new JLabel(images);

jScrollPane3.getViewport().add(lable);​

May you help me?

Thanks.

Mahsa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top