0

I have got a ImageIcon in a Jlabel in a JFrame (Java GUI).

The ImageIcon should get updated based on pressing a Calculate button (i.e. calcButton.addActionListener(new ActionListener() ) with part of the code in the method:

icon2 = new ImageIcon("M:\\Repos\\rtrans\\radTransPlot.png");
Plot1.setIcon(icon2);
frame.add(Plot1,gc);
frame.setVisible(true);

The initial ImageIcon (icon1) is blank:

public class RadTransGui 
{
private ImageIcon icon1 = new ImageIcon("M:\\Repos\\rtrans\\radTransPlotEmpty.png");
private ImageIcon icon2;
private JLabel Plot1 = new JLabel(icon1);

and gets properly updated based on the first Calculate button press but not after subsequent presses of Calculate button. The contents of M:\Repos\rtrans\radTransPlot.png gets updated correctly each time Calculate is pressed.

I have tried setting the ImageIcon to null and adding and removing the JLabel to the frame each time the Calculate button is pressed.

Any ideas? Thanks.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2one
  • 1,035
  • 3
  • 17
  • 36
  • you should share more code, including the code inside actionPerformed – Abhinav Chauhan Mar 31 '20 at 14:28
  • I printed the contents `System.out.println(Toolkit.getDefaultToolkit().getImage("M:\\Repos\\rtrans\\radTransPlot.png"));` after first *Calculate* press: `sun.awt.image.ToolkitImage@258903c8` and after another *Calculate* press: `sun.awt.image.ToolkitImage@258903c8` – 2one Mar 31 '20 at 15:01
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. .. – Andrew Thompson Apr 19 '20 at 04:06
  • 1
    .. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Apr 19 '20 at 04:06

1 Answers1

4

The constructor of ImageIcon() internally uses Toolkit.getDefaultToolkit().getImage.

You have to manually use Toolkit.getDefaultToolkit().createImage instead of Toolkit.getDefaultToolkit().getImage. The latter uses cache whereas the former doesn't and always returns a new instance.

new ImageIcon(Toolkit.getDefaultToolkit().createImage("..filename.."))

From the javadoc of createImage:

The returned Image is a new object which will not be shared with any other caller of this method or its getImage variant.

Compare with the javadoc of getImage:

The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image. [...] If the image data contained in the specified file changes, the Image object returned from this method may still contain stale information which was loaded from the file after a prior call.

There seems to be no javadoc or spec that prescribes that ImageIcon should use cached images, so it's a perfect example of how fragile programming is if you don't know 100% what you're doing. Even if it works in one environment doesn't guarantee it always works.

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50
  • Brilliant using: `icon2 = new ImageIcon(Toolkit.getDefaultToolkit().createImage("M:\\Repos\\rtrans\\radTransPlot.png"));` worked! Thank you! – 2one Mar 31 '20 at 15:10
  • Is the fact that the image is stored on disk *important*? If you just wanted to update the *displayed* image you don't need to write it to file first, but I just realized I don't know if you know that. – Mark Jeronimus Apr 01 '20 at 07:50
  • Thanks for the thought. At the moment yes the images are stored on disk as is the intention. Of course if I was dealing with `ImageIcon` images in memory only this would be different. – 2one Apr 01 '20 at 08:07