I'm recording AWT events (mouse_pressed
, mouse_released
, key_pressed
, key_released
, etc.) to a log to replay them back using Robot in unit tests. But I found that I sometimes need to insert mouse_released
events when they are missing, because some of my components remove themselves on mouse_pressed
so the mouse_released
is never dispatched. I thought that a good approach would be to insert mouse_released
whenever a mouse event is seen that has modifiersEx
=Button1
followed by an event that has modifiersEx
=0, unless the second event is already mouse_released. But I found a problem when using a JComboBox
.
Here's a simple main function containing a JComboBox
with a component under it that also receives mouse events.
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.InputEvent;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DemoEvents {
public static void main(String[] argv) {
JFrame jframe = new JFrame("Test events");
jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container contentPane = jframe.getContentPane();
contentPane.setLayout(new BorderLayout());
JComboBox jcomboBox = new JComboBox(new String[]{"one", "two", "three"});
JButton jbutton = new JButton("Hello");
JPanel outerPanel = new JPanel();
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.PAGE_AXIS));
innerPanel.add(jcomboBox);
innerPanel.add(jbutton);
outerPanel.add(innerPanel);
contentPane.add(outerPanel, BorderLayout.CENTER);
jframe.setSize(200, 200);
jframe.setVisible(true);
long mask =
AWTEvent.MOUSE_EVENT_MASK |
AWTEvent.MOUSE_WHEEL_EVENT_MASK |
AWTEvent.MOUSE_MOTION_EVENT_MASK;
final Logger logger = Logger.getLogger("awt-events");
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
InputEvent ev = (InputEvent)event;
logger.info(ev.toString());
}
}, mask);
}
}
When clicking on the JComboBox
and clicking item "one", I get these events. The mouse_entered
with extModifiers=Button1 doesn't make sense, because it was dispatched after the mouse_released event
! Here's a simplified log from when I clicked the combo box, then clicked the first item. Look at the bolded event at the bottom:
- MouseEvent[MOUSE_PRESSED,(1,13),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on MetalComboBoxButton[...]
- MouseEvent[MOUSE_RELEASED,(1,13),button=1,modifiers=Button1,clickCount=1] on MetalComboBoxButton[...]
- MouseEvent[MOUSE_MOVED,(0,14),button=0,clickCount=0] on MetalComboBoxButton[...]
- MouseEvent[MOUSE_EXITED,(-2,15),button=0,clickCount=0] on MetalComboBoxButton[...]
- MouseEvent[MOUSE_ENTERED,(81,15),button=0,clickCount=0] on JComboBox[...]
- MouseEvent[MOUSE_MOVED,(81,15),button=0,clickCount=0] on JComboBox[...]
- (more moves)
- MouseEvent[MOUSE_EXITED,(69,24),button=0,clickCount=0] on JComboBox[...]
- MouseEvent[MOUSE_ENTERED,(69,0),button=0,clickCount=0] on ComboPopup.popup
- MouseEvent[MOUSE_MOVED,(69,0),button=0,clickCount=0] on ComboPopup.popup
- MouseEvent[MOUSE_EXITED,(68,2),button=0,clickCount=0] on ComboPopup.popup
- MouseEvent[MOUSE_ENTERED,(67,1),button=0,clickCount=0] on ComboBox.list
- MouseEvent[MOUSE_MOVED,(67,1),button=0,clickCount=0] on ComboBox.list
- (more moves)
- MouseEvent[MOUSE_PRESSED,(57,9),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on ComboBox.list
- MouseEvent[MOUSE_RELEASED,(57,9),button=1,modifiers=Button1,clickCount=1] on ComboBox.list
- MouseEvent[MOUSE_ENTERED,(25,10),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on JButton[...]
- MouseEvent[MOUSE_MOVED,(26,10),button=0,clickCount=0] on JButton[...]
Question: When is the order of mouse_pressed
/mouse_released
inconsistent with the modifiers of other mouse events? Is it only mouse_entered
/mouse_exited
events that happen with inconsistent modifiers? Does this only happen when clicking onJComboBox
's popup?
I'm running Java 1.6 on Ubuntu.
Edit: included a longer log for clarity.