Loading too much images cause some problems

Hi,

I try to load multiple images from the file chooser. And display it in JScrollPane. I do repaint(); and validate(); inside my actionPerformed(); I am not too sure of these two functions actually. But I thought it should repaint the background after loading each image. In fact when I try to load 100s of images, it tooks quite along time to see the images in the JScrollPane and all the images are displayed at once. I wish to know if there's anyway to update the display after each image has been loaded. So that at least I can give the user the hint that the images are still loading.

Here's my problem code

publicvoid actionPerformed(ActionEvent e){

if (e.getSource() == BtnBrowse){

int returnVal = fcSelectFile.showDialog(this,"Select");

if (returnVal == JFileChooser.APPROVE_OPTION){

File file[]= fcSelectFile.getSelectedFiles();

//This is where a real application would open the file.

int count=0;

//PnlUpload.removeAll();

JLabel selectedFile[]=new JLabel[file.length];

while(count<file.length)

{

String actualFile=file[count].toString();

String fileName= file[count].getName();

selectedFile[count]=new JLabel(createImageIcon(actualFile,fileName));

PnlImageHolder.add(selectedFile[count]);

PnlImageHolder.repaint();

PnlImageHolder.validate();

count++;

}

ScpImageScroller.getViewport().add(PnlImageHolder);

}else{

log.append("Open command cancelled by user.\n");

}

}

}

//Here i get the images and resize it

protectedstatic ImageIcon createImageIcon(String path,

String description){

//java.net.URL imageURL = FotoUpload.class.getResource(path);

String imageURL = path;

if (imageURL ==null){

System.err.println("Resource not found: "

+ path);

returnnull;

}else{

ImageIcon icon=new ImageIcon(imageURL);

Image img=icon.getImage();

int width=img.getWidth(null);

int height=img.getHeight(null);

if(width>height)

{width=120;

height=90;

}

else

{width=90;

height=120;

}

Image thumbnail=img.getScaledInstance(width,height,java.awt.Image.SCALE_SMOOTH);

returnnew ImageIcon(thumbnail, description);

}

}

[3926 byte] By [pandoramica] at [2007-10-3 10:58:13]
# 1

I do repaint(); and validate(); inside my actionPerformed(); I am not too sure of these two functions actually. But I thought it should repaint the background after loading each image

see http://forum.java.sun.com/thread.jspa?threadID=785094

Using disk io from an ActionPerformed block is not recommended. Your program blocks the Event Thread and stops until all the images are loaded.

. I wish to know if there's anyway to update the display after each image has been loaded

As I remarked in a previous post, if you use a MediaTracker you can waitForID() and get a clear indication of which images have loaded and which have not. You will need to construct a Thread to load the images, and the Thread can periodically report on progress. Meanwhile, a painting thread could be woken up every time an image is loaded.

regards

messengersa at 2007-7-15 6:24:29 > top of Java-index,Security,Cryptography...
# 2

Hey.. you've been kept answering quite a few of my questions related to image. Thanks alot. I am quite newbie to java and I am a bit lost. Anyway, I go and see the link you refer to. I don't know is it just me or anything but somehow I really didn't see anything you talk about MediaTracker. Anyway, if I am not suppose to do the whole image loading process in ActionPerformed.. what is the advisable walk around to it? And can you show me any coding snippet for the MediaTracker? I am kinda lost.

pandoramica at 2007-7-15 6:24:29 > top of Java-index,Security,Cryptography...
# 3
http://forum.java.sun.com/thread.jspa?threadID=789506
messengersa at 2007-7-15 6:24:29 > top of Java-index,Security,Cryptography...
# 4

MediaTracker tracker = new MediaTracker (this) ; //this refers to the applet

Image img = Toolkit.getDefaultToolkit().getImage(file[count].toString());

tracker.addImage(img, SOME_IDENTIFYING_INTEGER);

try {

tracker.waitForID(SOME_IDENTIFYING_INTEGER);

//or if there is more than one image use tracker.WaitForAll();

} catch (InterruptedException e) {

}

What is that SOME_IDENTIFYING_INTEGER? ha ha.. sorry. :D Which Integer means what?

pandoramica at 2007-7-15 6:24:29 > top of Java-index,Security,Cryptography...
# 5

Using disk io from an ActionPerformed block is not recommended. Your program blocks the Event Thread and stops until all the images are loaded.

I heard some ppl say about not to do heavy operation on ActionPerformed or else it will block the GUI. But I am not exactly sure how exactly I can dispatch another thread from actionperformed and notify the actionperformed to update GUI as the operation progress? And I heard threaded need to be clean up etc. I don't have good example on how to go about doing it. This GUI blocking is really a big problem for me. I added a progress bar thinking it will actually show the progress. But Progress bar also only update after the whole operation has finished. So it doesn't make any differences

pandoramica at 2007-7-15 6:24:29 > top of Java-index,Security,Cryptography...