radekEm
radekEm

Reputation: 4647

Java Swing - isRollover() value in JButton model

I was testing the ButtonModel's behaviour and I came across a strange situation. The isRollover() method does not return the expected (according to me) value.

Here is the sample code I've just created:

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author rmu
 */
public class NewClass extends JFrame {

    private static final Logger logger = Logger.getLogger(NewClass.class.getName());

    static {
        logger.setLevel(Level.INFO);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Test MVC");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JButton btn = new JButton("Test button");
        final JLabel lbl = new JLabel("");

        btn.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                logger.log(Level.INFO, "\nButton armed: " + btn.getModel().isArmed()
                        + "\nButton pressed: " + btn.getModel().isPressed()
                        + "\nButton selected: " + btn.getModel().isSelected()
                        + "\nButton rollover: " + btn.getModel().isRollover()
                );
                lbl.setText(btn.getModel().isRollover() ? "Mouse is over the button" : "Mouse is NOT over the button");
            }
        });

        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
        frame.getContentPane().add(btn);
        frame.getContentPane().add(lbl);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Repo steps:

Is it correct behaviour or a bug? I wanted to write some code which uses this property and I'm not sure if I can rely on it.

Upvotes: 1

Views: 548

Answers (2)

camickr
camickr

Reputation: 324197

Is it correct behaviour or a bug?

Check out the source code to see what it is doing.

In the BasicButtonListener you have this code:

public void mouseEntered(MouseEvent e) {
    AbstractButton b = (AbstractButton) e.getSource();
    ButtonModel model = b.getModel();
    if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) {
        model.setRollover(true);
    }
    if (model.isPressed())
            model.setArmed(true);
}

So the rollover is not set when the left mouse is pressed.

Just use your own MouseListener and listen for mouseEntered/mouseExited events.

Upvotes: 2

Dakshinamurthy Karra
Dakshinamurthy Karra

Reputation: 5473

I wouldn't depend on the rollover behaviour. It is not really standardised in the sense that the L&F toolkits can(& some of them do) modify its behaviour.

That said, it should be trivial to implement your own mouse listener to get the behaviour you want.

Upvotes: 1

Related Questions