Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Problem To Update Jlabel With A Different Image..

Recommended Posts

Hi All,

I am inplementing a preview function on a image file browser. The application displays a list of image file names, which the user can select (one at a time). The preview image is loaded into a JLabel. When an image file name is selected, the application creates a thumbnail image on the hard drive call "preview.jpg". The JLabel should then update itself with the newly created thumbnail "preview.jpg".

For some reason I cannot get the Image in the JLabel to update. I have confirmed that "preview.jpg" is being rewritten, each time a new image file is selected from the list. It just wont update the JLabel image. I suspect the problem is due to the fact that the "preview.jpg" file name remains unchanged even though it has been rewritten (i.e. contains a new image). Is there some way I can force the JLabel to reload "preview.jpg" (Assuming that is the problem)?

public void valueChanged(ListSelectionEvent e)  {  	  	System.out.println("Selected Image Index: " + selectedFilesList.getSelectedIndex());  	if (selectedFilesList.getSelectedIndex() == -1) { //Prevents an exception error caused when no item has been selected from the file list.     System.err.println("Notice: returned index of -1!");    return; // Escapes from executing the rest of the method  	}  	  	File f = ((GalleryImage)imageFiles.elementAt(selectedFilesList.getSelectedIndex())).getImageFile();  	  	System.out.println("Selected image: " + f.getPath());  	try {    Thumbnail.createImage(f, "preview.jpg", 90, 60);    previewIcon = new ImageIcon("preview.jpg");    previewLabel.setIcon(previewIcon);    previewLabel.repaint();  	}  	catch(Exception e2) {    // TODO Add Exception handling code.  	}  }

BY,kvarnerexpress

Share this post


Link to post
Share on other sites

Don't know what's wrong, but you could try to make it switch to a different icon and then back to preview. That should make it repaint.

grayIcon = new ImageIcon("gray.jpg"); //Where gray.jpg is just a gray image (default color for labels and stuff is 204,204,204 or #CCCCCC)previewLabel.setIcon(grayIcon);previewLabel.repaint();Thumbnail.createImage(f, "preview.jpg", 90, 60);previewIcon = new ImageIcon("preview.jpg");previewLabel.setIcon(previewIcon);previewLabel.repaint();

Share this post


Link to post
Share on other sites

I am implementing a preview function on a image file browser. The application displays a list of image file names, which the user can select (one at a time). The preview image is loaded into a JLabel. When an image file name is selected, the application creates a thumbnail image on the hard drive call "preview.Jpg". The JLabel should then update itself with the newly created thumbnail "preview.Jpg".

-reply by PARTHEE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.