views:

27

answers:

1

I added an AWTEventListener to process grab event. So this listener just use sun.awt.SunToolkit.GRAB_EVENT_MASK mark. But This listener can not capture UngrabEvent. The tricky thing is, when a JComboBox popuped its menulist, it can capture this event. I use the following code for testing. Start the program, click on the empty area of the frame, click on the frame title. Then there should be an UngrabEvent. But the listener does not capture it. Start the program, click on the combobox and make its menulist popuped. click on the frame title. Then there should be an UngrabEvent. And the listener captures it. It is very strange...Is there any relationship between UngrabEvent and JComboBox?

public class ComboboxLearn {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        toolkit.addAWTEventListener(new AWTEventListener() {

            @Override
            public void eventDispatched(AWTEvent event) {
                System.out.println(event);
            }
        }, sun.awt.SunToolkit.GRAB_EVENT_MASK);
        JComboBox box = new JComboBox(new Object[] { "AAA", "BBB", "CCC" });

        box.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                System.out.println(e);
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                System.out.println(e);// Set a breakpoint here
            }

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                System.out.println(e);
            }
        });

        JFrame f = new JFrame();
        f.getContentPane().setLayout(new FlowLayout());
        f.getContentPane().add(box);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(new Dimension(100, 100));

        f.setVisible(true);
    }
}
A: 

I sense you're experimenting; but generally, you shouldn't rely on Sun/Oracle's undocumented APIs. A similar problem appears to be discussed here. It's apparently the root cause of this workaround.

Addendum:

I want a popup that will hide when the mouse is pressed outside the popup but not hide when the mouse is pressed on the popup.

Why not bring up a JDialog when you see isPopupTrigger() and hide it when you see it deactivating, as another window activates? The notion is discussed here.

trashgod
It is glad to seem some discussion on this problem.In the discussion, the problems is not resolved. And the workaround it not about Popup, it is about PopupMenu.
DeepNightTwo
Yes, it looks rather platform specific. I'd consider another approach using the standard API, as suggested above.
trashgod