Im trying to move picture used on one button to another button, i tried

Icon pic = btnA1.getIcon();
btnA1.setIcon(pic);

And btnA2 is not displaying "pic"? Or are you expecting btnA1 to no longer show that pic as well?

Comments
Just because you deserve it too :) Helping out new people can sometimes be a pain. :p

btnA2 is not displaying pic and btn A1 still displays pic
i want btn A1 to not diplay pic and btnA2 to diplay pic

Ok, we're not getting anywhere... I've decided to throw together some lines of code that apparently do just what you want. How convenient...

public class TestClass extends JPanel implements MouseListener {
    
    JLabel one;
    JLabel two;
    
    public TestClass () {
        super(new GridLayout(0,1));
        one = new JLabel(new ImageIcon(TestClass.class.getResource("images/img.jpg")));
        two = new JLabel("Test");
        add(one);
        add(two);
        addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e) {
        two.setIcon(one.getIcon());
        one.setIcon(null);
        one.setText("test");
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    
    
    
    public static void main (String[] args) {
        JFrame window = new JFrame("test");
        window.setContentPane(new TestClass());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.pack();
        window.setVisible(true);
    }
   
}

I have two labels in my panel, and when you click with your mouse it changes. To do this I worked with a MouseListener. Whenever the pmouse clicks the panel the method mouseClicked() is called. In there you find how I switched the icon.

Try to implement it and if you have questions, post them.

Black Box

Comments
hehe, you beat me to the example :)

ok i tried that and it works but i cant figure out how to get a preset icon from a form component, i already know how to set an icon, i just need the ysntax to take a previously stored icon.

and i have a 2-dimensional grid that has all 64 JToggleButtons coded into it, it's a chessgame and this is for move pics of piece, so i want to be able to move one piece from one toggle button to another toggle button

What do you mean by previously stored icon? If it has been previously stored, you just insert it where I get my icon. As for the JToggleButtons, yes, read the API on that class.

By the way, there is an "edit" button under each post... might want to use that one instead of raising your post count. :)

And try to implement as much as you can, then post the bit of code you're having trouble with. I think I was pretty kind to just give you that previous code, that only means you'll have to work harder to get another bit. Do some coding, and post the problem part on here. For the love of the god you prefer, don't post everything.

Black Box

as in in my form development the pieces are already set with icons, btw i dont set my frames using code i use a form editor, and API?????

So you retrieve the icon from the one button just like Black Box showed in his example. I'm not understanding what you want to do differently.

ill have to get to my code to see but i get some error thanks for your helps guys

This article has been dead for over six months. Start a new discussion instead.