Change the image in JLabel according to the button on which the cursor moves?

advertisements

I have created a Swing interface where I want an icon to appear in a JLabel when the cursor is hovered over a button. The icon will change based in which button the cursor hovers over.

Here is the coding I am using at the moment:

public void iconchange()
    {
        if(btnPlay.isRolloverEnabled() == true)
        {
            lblIcon.setIcon(new ImageIcon("Images/Play-icon.png"));
        }
    }

I know the coding above is wrong, so what can I do to achieve the feature I mentioned above?


Use a ChangeListener on the JButton's ButtonModel to monitor changes to the models state, update your UI based on the state of the model, for example...

public class ChangeHandler implements ChangeListener {

    private JLabel label;
    private Icon armedIcon;

    public ChangeHandler(JLabel label, Icon armedIcon) {
        this.armedIcon = armedIcon;
        this.label = label;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        ButtonModel model = (ButtonModel) e.getSource();
        if (model.isRollover()) {
            label.setIcon(armedIcon);
        } else {
            label.setIcon(null);
        }
    }

}

This will update the supplied JLabel, changing it's icon to the specified icon when the ButtonModel it's attached to is in the rollOver state

This could be used something like this...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class RollOver {

    public static void main(String[] args) {
        new RollOver();
    }

    public RollOver() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            // I'm doing this, because I don't have blank icon of 128x128 and my
            // icons are both 128x128
            label = new JLabel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(128, 128);
                }
            };
            add(label, gbc);
            try {
                JButton btn = new JButton("Bunnies");
                btn.getModel().addChangeListener(new ChangeHandler(label, new ImageIcon(ImageIO.read(getClass().getResource("/Bunny.png")))));
                add(btn);
                btn = new JButton("Zomnies");
                btn.getModel().addChangeListener(new ChangeHandler(label, new ImageIcon(ImageIO.read(getClass().getResource("/Zombi.png")))));
                add(btn);
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

        public class ChangeHandler implements ChangeListener {

            private JLabel label;
            private Icon armedIcon;

            public ChangeHandler(JLabel label, Icon armedIcon) {
                this.armedIcon = armedIcon;
                this.label = label;
            }

            @Override
            public void stateChanged(ChangeEvent e) {
                ButtonModel model = (ButtonModel) e.getSource();
                if (model.isRollover()) {
                    label.setIcon(armedIcon);
                } else {
                    label.setIcon(null);
                }
            }

        }

    }

}

Take a closer look at How to Use Buttons, Check Boxes, and Radio Buttons for more details