# Updating image in JLabel



## gdi1942 (Nov 2, 2006)

I'm learning java in school and am making a card game. I used a GUI builder to make the screen. and each card in your hand is represented by card0, card1, ect. Now my problem lies in the fact that i can't update a JLabel image with code. I have all my card's layed out and i've made a small snippet of code to try to change the card immediatly during the building of the GUI

==WORKS==
public void run() {
card0.setIcon(new javax.swing.ImageIcon("pic/MISC/MISC 001.jpg"));
new GUI().setVisible(true);
}

==DOESN'T WORK==
public void run() {
new GUI().setVisible(true);
card0.setIcon(new javax.swing.ImageIcon("pic/MISC/MISC 001.jpg"));
}

I think the problem lies in not updating the GUI but i don't know how to do that.

The cards have all been set to a default picture when the gui loads.


----------



## Chicon (Jul 29, 2004)

Hi gdi1942,

Did you try the *updateUI* method on your object *card0* ?

If it doesn't work, you may also try to run a new thread this way :


```
[SIZE=2]
public void run() {
    new GUI().setVisible(true);
    Thread t = new Thread(new Runnable() {
        public void run() {
            card0.setIcon(new javax.swing.ImageIcon("pic/MISC/MISC 001.jpg"));
        }
    });
    t.start();
}
[/SIZE]
```


----------



## gdi1942 (Nov 2, 2006)

yea that didn't work, still it's default picture

my code now looks like this
public void run() {

new GUI().setVisible(true);
card0.setIcon(new javax.swing.ImageIcon("pic/MISC/MISC 001.jpg"));
card0.updateUI();
}

The GUI builder i used did make somethin very simalar to what you wrote above, a "thread". We haven't gotten to threads in class yet, so i'd rather stay away from that. So i copied the method inside the the thread into the class itself. UpdateUI made no change, anything else i can try, i've also e-mailed my programming teacher.

The code there for card0 won't stay there, i only put it there because i need to figure out how to change cards without needing the main class to do it. I plan to change the cards images through a method. But this needs to be resolved first.


----------



## Chicon (Jul 29, 2004)

Generally to load images, JAVA needs separate threads.
Normally, *card0* is a jLabel and there's an inherited method called *repaint()*.
Instead of *updateUI()* method, try to use *repaint()*.


----------



## gdi1942 (Nov 2, 2006)

Nope code now looks like this

new GUI().setVisible(true);
card0.setIcon(new javax.swing.ImageIcon("pic/MISC/MISC 001.jpg"));
card0.repaint();

it's stupid because it works if i change the setIcon attribute before i make it visible, but not after.


----------



## Chicon (Jul 29, 2004)

After the card *card0.repaint();* instruction, add the *card0.validate();* instruction.
If it doesn't work, just have a read on the following paragraph.
On this page, there's a good example of refreshing a jLabel object with a picture.
Notice the instruction *pack()* in the method *refresh* of the class *PictureFrame*, that instruction works only on Frames.


----------



## gdi1942 (Nov 2, 2006)

code did not work, that method is very complex to me.

I guess i'll just have to wait and see what my programming teacher says. Maybe she'll know what to do.


----------



## Chicon (Jul 29, 2004)

gdi1942 said:


> code did not work, that method is very complex to me.
> 
> I guess i'll just have to wait and see what my programming teacher says. Maybe she'll know what to do.


I'm having a try tomorrow with my IDE. I'll post if I find something to solve the problem.


----------



## Chicon (Jul 29, 2004)

The general rule says that Java Swing & Awt components can be altered while treating an event (key pressed, mouse clicked, ...).
I know I should have asked you that question in a first time : what event are you treating in your program ?
BTW, I developped a small application that uses events triggered by both the computer (Java Swing component *Timer*) and the user (button clicked).
When the user clicks on the button, each smiley used by this forum is displayed in a *JLabel* at a frequency of a picture by each half second.


```
[SIZE=2]
package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TsgIcons extends JFrame implements ActionListener {
    protected Timer timer;
    protected JLabel viewIcon;
    private String[] SMILEY = {"biggrin","confused","cool","eek","frown",
                            "mad","redface","rolleyes","smile","tongue","wink"};
    private String BUTTON = "Button";
    private int timeCount;
    private int iconNumber;
    private String image;
    
    public TsgIcons() {
        this(0, 500);
    }
    
    public TsgIcons(int initialTime, int delay) {
        super("TSG Smileys");
        this.timeCount = initialTime;
        this.iconNumber = this.timeCount % this.SMILEY.length;
        this.image = "images\\" + this.SMILEY[this.iconNumber] + ".gif";
        this.viewIcon = new JLabel();
        this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));
        this.timer = new Timer(delay, this);
        this.init();
    }
    
    protected void init() {
        JButton button = new JButton("Start / Stop");
        button.setActionCommand(BUTTON);
        button.addActionListener(this);
        this.viewIcon.setHorizontalAlignment(JLabel.CENTER);
        this.getContentPane().add(button, BorderLayout.SOUTH);
        this.getContentPane().add(this.viewIcon, BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(250, 250);
        this.pack();
        this.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        if ( BUTTON.equals(e.getActionCommand()) )  { // test if the button clicked
            if ( this.timer.isRunning() ) {
                this.timer.stop();
            } else {
                this.timer.start();
            }
        } else
        {	this.timeCount++;
                this.iconNumber = this.timeCount % this.SMILEY.length;
                this.image = "images\\" + this.SMILEY[this.iconNumber] + ".gif";
                this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));
        }
    }
   
    public static void main(String argv []) {
        new TsgIcons();
    }
}

[/SIZE]
```


----------

