1

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:

  • Move the mouse over the button --> Everything's OK
  • Press the mouse button --> Everything's OK
  • (without releasing the mouse button) Move the mouse so, that it's no longer over the button --> Everything's OK
  • (without releasing...) Move the mouse over the button again --> isRollback() method returns false (see logs and JLabel)

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
radekEm
  • 4,617
  • 6
  • 32
  • 45
  • [maybe this is a bug](http://stackoverflow.com/q/30581812/714968), to use SwingUtilities.isLeftMouseButton – mKorbel Sep 07 '15 at 17:20

2 Answers2

2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

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.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28