Avatar billede fredand Forsker
29. april 2003 - 10:16 Der er 6 kommentarer og
2 løsninger

Switching ImageIcon in a JLabel??

Hello!

I trying to make a app that should be able to switch among images. For that I use JLabel with Imagecon but the image doesn't switch.

I got the code below so you can test it quick.

So if any one can se why the JLabel doesn't update when I push the button please let me know.

Best Regards Fredrik

Ps To test it you need 2 images 1.gif and 2.gif in a mapp called images

The code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ChangeIconTest extends JFrame implements ActionListener
{
    int image = 1;
    JPanel jPanel;
    JButton jButton;
    JLabel jLabel;

    public ChangeIconTest()
    {
        jPanel = new JPanel();
        jButton = new JButton("Shift");
        jButton.addActionListener(this);
        jLabel = new JLabel(new ImageIcon("images/1.gif"));

        jButton.setBounds(10, 10, 100, 20);
        jLabel.setBounds(10, 40, 100, 100);

        jPanel.add(jLabel);
        jPanel.add(jButton);

        jPanel.setBounds(0, 0, 200, 250);
        jPanel.setLayout(null);

        getContentPane().setLayout(null);
        getContentPane().add( jPanel );

        setSize(200, 277);
        show();
    }

    public void shift()
    {
        if(image == 1)
        {
            image = 2;
            jLabel = new JLabel(new ImageIcon("images/2.gif"));
        }
        else if(image == 2)
        {
            image = 1;
            jLabel = new JLabel(new ImageIcon("images/1.gif"));
        }
        System.out.println(image);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == jButton)
        {
            shift();
        }
    }

    public static void main(String args[])
    {
        ChangeIconTest changeIconTest = new ChangeIconTest();
    }
}
Avatar billede soreno Praktikant
29. april 2003 - 10:20 #1
Have you tried:
jLabel.setIcon(new ImageIcon("images/1.gif"));

?
Avatar billede arne_v Ekspert
29. april 2003 - 10:30 #2
You need to remove the old JLabel object and add the new JLabel object.
Avatar billede arne_v Ekspert
29. april 2003 - 10:33 #3
What you do is:

you assign reference to a new JLabel
you add that reference to the GUI (which saves that referece)
you assign a new reference to a new label

then you jLabel variable do contain the new reference, but the GUI
still contains the old reference.
Avatar billede arne_v Ekspert
29. april 2003 - 10:34 #4
BTW, Sørens solution is much easier to implement, so use that. The
above is just an explanation of why your code does not behave as you expect.
Avatar billede fredand Forsker
29. april 2003 - 10:34 #5
Hello Guys!

Thanks for your reply!

I tried both of your solutions and sorenos worked fine but not this one if I got it right?

    public void shift()
    {
        if(image == 1)
        {
            image = 2;
            jPanel.remove(jLabel);
            jLabel = new JLabel(new ImageIcon("wwwfileloader/images/2.gif"));
            jLabel.setBounds(10, 40, 100, 100);
            jPanel.add(jLabel);
            //jLabel.setIcon(new ImageIcon("wwwfileloader/images/2.gif"));
        }
        else if(image == 2)
        {
            image = 1;
            jPanel.remove(jLabel);
            jLabel = new JLabel(new ImageIcon("wwwfileloader/images/1.gif"));
            jLabel.setBounds(10, 40, 100, 100);
            jPanel.add(jLabel);
            //jLabel.setIcon(new ImageIcon("wwwfileloader/images/1.gif"));


        }
        System.out.println(image);
    }
Avatar billede arne_v Ekspert
29. april 2003 - 10:52 #6
public void shift()
    {
        if(image == 1)
        {
            image = 2;
            jPanel.remove(jLabel);
            jLabel = new JLabel(new ImageIcon("wwwfileloader/images/2.gif"));
            jLabel.setBounds(10, 40, 100, 100);
            jPanel.add(jLabel);
            repaint(); // <------------------
            //jLabel.setIcon(new ImageIcon("wwwfileloader/images/2.gif"));
        }
        else if(image == 2)
        {
            image = 1;
            jPanel.remove(jLabel);
            jLabel = new JLabel(new ImageIcon("wwwfileloader/images/1.gif"));
            jLabel.setBounds(10, 40, 100, 100);
            jPanel.add(jLabel);
            repaint(); // <------------------
            //jLabel.setIcon(new ImageIcon("wwwfileloader/images/1.gif"));


        }
        System.out.println(image);
    }

should work.
Avatar billede arne_v Ekspert
29. april 2003 - 10:54 #7
And Sørens solution is definatetly what you want to use.

My way is more an exercise to illustrate what the problem was.
Avatar billede fredand Forsker
29. april 2003 - 11:13 #8
Hello!

Yes now I got to work both ways!

Thanks!

Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester