How to determine if the user will click on the window title or any other part of the window border in Java

I have implemented a popup numeric keypad using the Swing Popup class. I have a button associated with a JTextField that opens the numeric keypad when the user taps on it, and then when / if the JTextField loses focus, the Popup is closed. This usually works well, except that sometimes I get artifacts that are "left" from the popup after hiding it. Sometimes the artifact is an image of the Components that were shown in the pop-up, but more often it is a "black hole" that hides everything that is displayed in the same area of ​​the screen that the pop-up was on, which can be fixed by disabling the application and the JVM ...

The problem is difficult to reproduce, but it manifests itself when the user manipulates the base window while the popup is open, such as moving or resizing it. My thought is to just hide the popup when something like this happens, which I can do with a combination of WindowListener and ComponentListener. However, I would like to take it one step further and hide the popup as soon as the user similarly clicks on the title bar of a window or other part of their frame, even before they move, resize, or designate it. JComboBox popups actually work this way. However, I was unable to find any mechanism by which to get notified that the user has clicked on the title of the window.I have looked at the JComboBox and associated code and cannot figure out how it works with this magic. Is there any other listener I could use to receive such a notification?

+1


source to share


2 answers


I have implemented a popup numeric keypad using the Swing Popup class.

Please post well your code demonstrating the implementation and issue when posting a question.



I don't know exactly what you are doing, but you can use JPopupMenu

. This closes when you click on the frame title bar without a FocusListener or any additional logic.

+2


source


will be removed, just code for testing,

private boolean _myWindowFocusLost = false;

      



...

        _xxXxx.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {//Invoked when a component gains the keyboard focus.
                if (e.getOppositeComponent() != null) {
                    if (e.getOppositeComponent() instanceof JComponent) {
                        JComponent opposite = (JComponent) e.getOppositeComponent();
                        if ((opposite.getTopLevelAncestor() != _myPopupWindow) && (!_myWindowFocusLost)) {
                            _myWindowFocusLost = false;
                        }
                    }
                }
            }
        });

      

+2


source







All Articles