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 returnsfalse
(see logs andJLabel
)
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.