2

I am trying to display an image whose contents are changed after some time. I am displaying it in JLabel, but the problem is when I reload this image it's not changing in JLabel as seems JLabel#setIcon(new ImageIcon("myImagePath.png")); caches image in the memory and when I changes that it look for the name and did not load it from harddisk.

Even I use two Images to flip whose data contents are changed? Any one know how to fix that? But every time loading image with different name works fine?

Lines creating problem: E.g. My image changed on button click event

jlabel.setIcon("d:\\img.png");
jlabel.repaint();
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Shahrzad
  • 1,062
  • 8
  • 26
  • 2
    Just a note. You should read http://sscce.org, because what you have there is very far from being an SSCCE. – JB Nizet Oct 06 '13 at 08:57
  • I have seen [http://stackoverflow.com/questions/10445665/jlabel-refresh-icon-with-updated-image] but did not helped – Shahrzad Oct 06 '13 at 09:04
  • 2
    I found [this](http://stackoverflow.com/q/10318502/1057230), that might can help. Else, please see if this [answer](http://stackoverflow.com/a/15187181/1057230), helps somewhat. Though as already stated without SSCCE it's hard to say anything :-) – nIcE cOw Oct 06 '13 at 09:07
  • thakns *nIcE cOw* your first link also working well, but i solved thanks anyway -- 1 – Shahrzad Oct 06 '13 at 09:27

1 Answers1

6

I have solved the problem by using ImageIO

try {
    BufferedImage bufImg=ImageIO.read(new File("d:\\img.png"));
    jlabel.setIcon(new ImageIcon(bufImg));
    //jlabel.repaint();
    //works even without repaint
}
catch (IOException ex) {
    System.out.println("Unable to read image file");
}

Thanks nIcE cOw Also working with mKorbel's answer as

ImageIcon img=new ImageIcon("D:\\img.png");
img.getImage().flush();
jlabel.setIcon(img);

but don't know what is problem with setIcon() if i don't call img#getImage()#flush();. Any one know?

Also you can note that

  1. ImageIO is somehow slow
  2. img.getImage().flush(); calling this will flicker the image
  3. but if I have different file name every time no need to call img.getImage().flush(); only go on jlabel.setIcon(img); and image is not flickering.
Community
  • 1
  • 1
Shahrzad
  • 1,062
  • 8
  • 26
  • The images used by the application, will later become part of it on deployment, and hence refer to as embedded resource. Then it would be wise, to access them in terms of a `URL` instead of a `File`. Don't use `Absolute Path` instead use `Relative Path` with respect to the `.class/package`, using `ClassName.class.getResource("/pathToImage/image.extension")`, as shown in the [info](http://stackoverflow.com/tags/embedded-resource/info) of [tag:embedded-resource], do watch the links provided on the info page. For the rest You're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Oct 06 '13 at 12:40
  • 2
    `but don't know what is problem with setIcon(). Any one know?` - What problem? The image is cached so you need to create a new Icon as was demonstrated by mKobel's answer. – camickr Oct 06 '13 at 15:38
  • @nIcE cOw: I know this thing but my problem was to get external image, may be download from web and write in *temp* on same file. then show – Shahrzad Oct 08 '13 at 15:27
  • @aquestion : Actually `ImageIO.read()` blocks the flow, till the image is loaded, hence if the images you are trying to display are larger in size, try to do that task in the background using `SwingWorker`, as shown in this [example](http://stackoverflow.com/a/18032704/1057230) – nIcE cOw Oct 08 '13 at 16:04