Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Updating image in JLabel


(!)

gdi1942's Avatar
gdi1942 gdi1942 is offline
Member with 215 posts.
THREAD STARTER
 
Join Date: Nov 2006
Experience: Advanced
12-Nov-2006, 03:06 PM #1
Updating image in JLabel
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's Avatar
Computer Specs
Member with 6,650 posts.
 
Join Date: Jul 2004
Location: 50 34' 07.13" N - 04 10' 23.
Experience: Second socks retriever
12-Nov-2006, 03:23 PM #2
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 :

Code:

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();
}
gdi1942's Avatar
gdi1942 gdi1942 is offline
Member with 215 posts.
THREAD STARTER
 
Join Date: Nov 2006
Experience: Advanced
12-Nov-2006, 03:30 PM #3
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's Avatar
Computer Specs
Member with 6,650 posts.
 
Join Date: Jul 2004
Location: 50 34' 07.13" N - 04 10' 23.
Experience: Second socks retriever
12-Nov-2006, 03:46 PM #4
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's Avatar
gdi1942 gdi1942 is offline
Member with 215 posts.
THREAD STARTER
 
Join Date: Nov 2006
Experience: Advanced
12-Nov-2006, 03:49 PM #5
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's Avatar
Computer Specs
Member with 6,650 posts.
 
Join Date: Jul 2004
Location: 50 34' 07.13" N - 04 10' 23.
Experience: Second socks retriever
12-Nov-2006, 04:29 PM #6


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's Avatar
gdi1942 gdi1942 is offline
Member with 215 posts.
THREAD STARTER
 
Join Date: Nov 2006
Experience: Advanced
12-Nov-2006, 04:44 PM #7
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's Avatar
Computer Specs
Member with 6,650 posts.
 
Join Date: Jul 2004
Location: 50 34' 07.13" N - 04 10' 23.
Experience: Second socks retriever
12-Nov-2006, 04:50 PM #8
Quote:
Originally Posted by gdi1942
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's Avatar
Computer Specs
Member with 6,650 posts.
 
Join Date: Jul 2004
Location: 50 34' 07.13" N - 04 10' 23.
Experience: Second socks retriever
13-Nov-2006, 06:42 AM #9
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.

Code:

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();
    }
}


Last edited by Chicon; 13-Nov-2006 at 01:42 PM..
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑