1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Updating image in JLabel

Discussion in 'Software Development' started by gdi1942, Nov 12, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. gdi1942

    gdi1942 Thread Starter

    Joined:
    Nov 1, 2006
    Messages:
    215
    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.
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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:
    [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]
     
  3. gdi1942

    gdi1942 Thread Starter

    Joined:
    Nov 1, 2006
    Messages:
    215
    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.
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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().
     
  5. gdi1942

    gdi1942 Thread Starter

    Joined:
    Nov 1, 2006
    Messages:
    215
    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.
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    :confused:

    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.
     
  7. gdi1942

    gdi1942 Thread Starter

    Joined:
    Nov 1, 2006
    Messages:
    215
    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.
     
  8. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    I'm having a try tomorrow with my IDE. I'll post if I find something to solve the problem.
     
  9. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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:
    [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]
     
  10. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/517708