2

It seems like i'm not the only one with that question but I can't find an answer that solves the problem.

I created a Label and assign an Icon to it using WYSIWYG interface designer.
Now I want to change the icon dynamically during runtime.

The logic way would be like this (my first attempt) :

ImageIcon newIcon = new ImageIcon("SomePath");
jLabel1.setIcon(newIcon); 

When I do this the Icon simply disapears from the interface so I googled it and someone said to "flush" the icon whatever this means I tried it :

ImageIcon newIcon = new ImageIcon("SomePath");
newIcon.getImage().flush();
jLabel1.setIcon(newIcon);

Still having the same problem.. The icon disapears.

What am I doing wrong ?


Update (Full Method) :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
attempted = myEngine.Attempt('q', word);
if(attempted)
  {
      this.jTextArea1.setText(myEngine.ChangeEncrypt('q', word, this.jTextArea1.getText()));    
  }
  else
  {
      JOptionPane.showMessageDialog(null,"The Letter Q is not in the word", "Error",JOptionPane.WARNING_MESSAGE);
      jButton1.setEnabled(false);
      life ++;
      ImageIcon newIcon = myEngine.UpdatePicture(life);
      newIcon.getImage().flush();
      jLabel1.setIcon(newIcon);
  }

This is the UpdatePicture Method :

public ImageIcon UpdatePicture(int life)
{
  ImageIcon emptyIcon = new ImageIcon();
  if (life == 0)
  {
       ImageIcon iconZero = new ImageIcon("/hang0.gif");
       return iconZero;
  }
  if (life == 1)
  {
      ImageIcon iconOne = new ImageIcon("/hang1.gif");
      return iconOne;
  }
  if (life == 2)
  {
      ImageIcon iconTwo = new ImageIcon("/hang2.gif");
      return iconTwo;
  }
  if (life == 3)
  {
      ImageIcon iconThree = new ImageIcon("/hang3.gif");
      return iconThree;
  }
  if (life == 4)
  {
      ImageIcon iconFour = new ImageIcon("/hang4.gif");
      return iconFour;
  }
  if (life == 5)
  {
      ImageIcon iconFive = new ImageIcon("/hang5.gif");
      return iconFive;
  }
  if (life == 6)
  {
      ImageIcon iconSix = new ImageIcon("/hang6.gif");
      return iconSix;
  }

  return emptyIcon;

}

Not sure the whole code was necessary but still it might help.

The life variable starts at 0.
I checked and in the UpdatePicture it hits the "/hang1.gif"; and returns it.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
phadaphunk
  • 12,785
  • 15
  • 73
  • 107
  • When you say it disappears, do you mean that it shows up at some point and then disappears? Or never shows up at all? Also, can you post where you call this code? – WilliamShatner Nov 07 '12 at 19:56
  • It never shows up. I meant the default one disapears and the other one don,t show up. Yes I will update with more code – phadaphunk Nov 07 '12 at 19:59
  • interesting `flush()` working in most cases and required for hight frequency, lots of periods pre seconds, are you able to show any image one time, don't load images on runtime, load that as local variable, then change icon (and if sin't changed to call for flush()) – mKorbel Nov 07 '12 at 20:09
  • AFAIK Icon ImageIcon never returns exceptions (by default), have to test for null, are you able to test for null value, because your path (I think) isn't correct – mKorbel Nov 07 '12 at 20:11
  • I tested and added an Icon as a local variable and it doesn't work it stays blank – phadaphunk Nov 07 '12 at 20:13
  • @PhaDaPhunk As mKorbel says, I would test the path to the file to check if it is null. Or temporarily add a hard path just to test. If that isn't the issue, possibly do a clean and build on the project. What you tried above would work if it was a caching issue. – WilliamShatner Nov 07 '12 at 20:14
  • to check @Gagandeep Bali profile, he posted a few very kind answers about packaging and Icon / ImageICon – mKorbel Nov 07 '12 at 20:14
  • Weird that you would need to call flush in a case like this. Are you sure updatePicture(int) doesn't return null? Then it would make sense that the image disappears. If not, are you sure that you're calling all SWING related code on the event dispatch thread (EDT)? It can often lead to weird unpredictable behavior if you don't - but your behavior seems to be consistent, so that's probably not it. (Style note: method names should start with a lower-case letter) UPDATE: Ok, was beaten to it :) – DisplayName Nov 07 '12 at 20:16
  • Where is your image precisely located ? – Mickäel A. Nov 07 '12 at 20:23
  • @mKorbel My bad. It works with the full path. So the path is the problem. How should I solve this ? Just the file name should refer to the src file no ? – phadaphunk Nov 07 '12 at 20:25
  • @miNde src folder. With all my .java – phadaphunk Nov 07 '12 at 20:26
  • @PhaDaPhunk [@Gagandeep Bali](http://stackoverflow.com/a/9866659/714968) (don't forget to update your answer(s)) – mKorbel Nov 07 '12 at 20:28

3 Answers3

3

If the file is in your src folder then :

ImageIcon ii = new ImageIcon(getClass().getResource("/myFile.gif"));
Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
1

You really should not put that slash before the name of your icon. It works like that.

gore_obsessed
  • 146
  • 2
  • 11
  • I just tried both ways and first time experienced the same sympthoms like you had, and it worked fine without slash :) Anyway, the best practice is to do like @miNde sugested. – gore_obsessed Nov 07 '12 at 20:33
0

I know this is an old question thread, but was having a hell of a time getting a icon replaced on a jLabel when my app was running. This is what finally fixed it: I created a new folder in the source package named images and put the images in there. In my Frame (main class) I added this below the initComponents method:

  private void moreComponents() {
//    get images for initial screen prompts (Skd2_startPanel, loading label1).
// StartPanel is a panel on the Frame that has the loading label on it))
try {
  lcImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/LC.png")));
  clImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/CL.png")));
} catch (IOException ex) {
  Logger.getLogger(Skd2_Frame.class.getName()).log(Level.SEVERE, null, ex);
 }
}

LC and CL.png are the images I want as the label icons.

In a different class I added the following when I wanted the icon to change:

   loadingLabel1.setIcon(lcImage); // or clImage as needed.

You'll need the following imports:

import java.util.logging.Logger;
import javax.imageio.ImageIO;

And add these two declarations just below the variables declarations:

 static ImageIcon lcImage;
 static ImageIcon clImage;

Hope this helps someone searching the web for an answer.

PiBurner
  • 75
  • 11