When are valid AWT modifiers (extended) guaranteed?

I write AWT-event ( mouse_pressed, mouse_released, key_pressed, key_releasedetc.) in the log to reproduce them using the Robot in the unit tests. But I found that sometimes I need to insert events mouse_releasedwhen they are absent, because some of my components are deleted on mouse_pressed, and therefore mouse_releasednever sent. I thought a good approach would be to insert mouse_releasedwhenever a mouse event occurs that has modifiersEx= Button1, followed by an event with modifiersEx= 0 if the second event is already output by the mouse. But I found a problem when using JComboBox.

Here is a simple main function containing JComboBoxwith a component below it that also accepts mouse events.

Screenshot of window with JComboBox

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 I click on JComboBoxand click on the "one" element, I get these events. mouse_enteredwith extModifiers = Button1 does not make sense since it was sent after mouse_released event ! Here's a simplified log when I clicked on the combo box, then clicked on the first item. Look at the bold event below:

  • 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] MetalComboBoxButton [...]
  • MouseEvent [MOUSE_ENTERED, (81,15), button = 0, clickCount = 0] JComboBox [...]
  • MouseEvent [MOUSE_MOVED, (81,15), button = 0, clickCount = 0] JComboBox [...]
  • ( )
  • MouseEvent [MOUSE_EXITED, (69,24), button = 0, clickCount = 0] JComboBox [...]
  • MouseEvent [MOUSE_ENTERED, (69,0), button = 0, clickCount = 0] ComboPopup.popup
  • MouseEvent [MOUSE_MOVED, (69,0), button = 0, clickCount = 0] ComboPopup.popup
  • MouseEvent [MOUSE_EXITED, (68,2), button = 0, clickCount = 0] ComboPopup.popup
  • MouseEvent [MOUSE_ENTERED, (67,1), button = 0, clickCount = 0] ComboBox.list
  • MouseEvent [MOUSE_MOVED, (67,1), button = 0, clickCount = 0] ComboBox.list
  • ( )
  • MouseEvent [MOUSE_PRESSED, (57,9), button = 1, = Button1, extModifiers = Button1, clickCount = 1] ComboBox.list
  • MouseEvent [MOUSE_RELEASED, (57,9), button = 1, = Button1, clickCount = 1] ComboBox.list
  • MouseEvent [MOUSE_ENTERED, (25,10), button = 1, = Button1, extModifiers = Button1, clickCount = 1] JButton [...]
  • MouseEvent [MOUSE_MOVED, (26,10), button = 0, clickCount = 0] JButton [...]

: mouse_pressed/mouse_released ? mouse_entered/mouse_exited, ? JComboBox popup?

Java 1.6 Ubuntu.

: .

+3
1

, JComboBox ", , .

com.apple.laf.AquaComboBoxButton, ComboBoxUI. MOUSE_CLICKED . JComboBox; , , .

, " , ALT, CTRL, META, ", extModifiers . , , MOUSE_MOVED .

, MOUSE_RELEASED. , , .

May 11, 2012 10:36:36 AM DemoEvents$1 eventDispatched
INFO: java.awt.event.MouseEvent[MOUSE_PRESSED,(85,9),absolute(124,58),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on com.apple.laf.AquaComboBoxButton[…]
May 11, 2012 10:36:36 AM DemoEvents$1 eventDispatched
INFO: java.awt.event.MouseEvent[MOUSE_RELEASED,(85,9),absolute(124,58),button=1,modifiers=Button1,clickCount=1] on com.apple.laf.AquaComboBoxButton[…]
May 11, 2012 10:36:36 AM DemoEvents$1 eventDispatched
INFO: java.awt.event.MouseEvent[MOUSE_CLICKED,(85,9),absolute(124,58),button=1,modifiers=Button1,clickCount=1] on com.apple.laf.AquaComboBoxButton[…]
May 11, 2012 10:36:38 AM DemoEvents$1 eventDispatched
INFO: java.awt.event.MouseEvent[MOUSE_ENTERED,(91,6),absolute(124,58),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on ComboBox.list
May 11, 2012 10:36:38 AM DemoEvents$1 eventDispatched
INFO: java.awt.event.MouseEvent[MOUSE_PRESSED,(91,6),absolute(124,58),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on ComboBox.list
May 11, 2012 10:36:38 AM DemoEvents$1 eventDispatched
INFO: java.awt.event.MouseEvent[MOUSE_RELEASED,(91,6),absolute(124,58),button=1,modifiers=Button1,clickCount=1] on ComboBox.list
+1

All Articles