• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to update an image in a Java GUI  RSS feed

 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a GUI with several buttons and I'm using NetBeans GUI Builder to do.
At the click of one of these I would like for it to open another frame containing a picture.

So I associate a listener (actionPerformed) the button and when clicked it opens actually post the new frame.

In the new frame I waxed a JLabel and then I associate the image of the label. I saw that to do that NetBeans generates this code:

label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/tree.png")));

My problem is that the picture is overwritten several times during the execution of the program is not changed yet in the frame.
That is, in the new frame is always displayed an old version of the image.

I have an image that is created every time I click on the button (it always has the same name and same path). Basically I have a generic tree on which I perform the operations (add, remove, etc..). When I click on the button I call a method that generates the image of the tree (using Graphviz). So I have one image that changes over the time...



How can I do so that the image is always up to date?
Thank you very much


The Code:

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Ugh, NetBeans-created Swing code is the worst. The problem is that the getResource approach will only load the resource once per lifetime of the JVM. If the file changes you need to read it via file operations, most easily using the javax.imageio.ImageIO.read methods.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Welcome to the Ranch.

Ugh, NetBeans-created Swing code is the worst. The problem is that the getResource approach will only load the resource once per lifetime of the JVM. If the file changes you need to read it via file operations, most easily using the javax.imageio.ImageIO.read methods.


Thank you! So if I write the code without using NetBeans builder what should I write?
it is enough if I create a label and associate an image or should I use other functions?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, using the NetBeans-generated code is OK since you have it already. It's just that it's very hard to read, and even harder to change manually later.

Most of the other ImageIcon constructors would be fine, I think, for example the one that takes a file name as a String. That one should read the file every time it is called, and not cache it.
 
Danny Wreczycki
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alicia Perry wrote:
Ulf Dittmer wrote:Welcome to the Ranch.

Ugh, NetBeans-created Swing code is the worst. The problem is that the getResource approach will only load the resource once per lifetime of the JVM. If the file changes you need to read it via file operations, most easily using the javax.imageio.ImageIO.read methods.


Thank you! So if I write the code without using NetBeans builder what should I write?
it is enough if I create a label and associate an image or should I use other functions?


Its really depends of your needs. Lets assume all you want is to just display image with orginal resolution without additional filters and so on. In order to do so you can just create new method called for example

public void changeImage(String pathToImage) {
label.setImage(pathToImage); // setup new imageicon for the label
label.repaint(); // repaint label so new image will be displayed
}

But if you want to perform additional tasks to image itself you should pick BufferedImage instead. In order to use BufferedImage instead of ImageIcon you need to subclass for example awt.Label and override paintComponent() method.

Take a look here http://www.coderanch.com/t/635062/GUI/java/Display-JPEG.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!