JWindow never receives focus events

I have a jwindow (set always on top) that you can click to get a pop menu. If the user right-clicks on the window, the popup menu will appear, but if the user clicks on any other window (like firefox), the pop pop menu will not disappear.

I tried to fix it by adding FocusListener to jwindow, I implemented FocusListener and override


    public void focusGained(FocusEvent e) {
    System.out.println("gain" );
    }

    public void focusLost(FocusEvent e) {
    System.out.println("lost" );
    }

      

but events are never called. I have also tried the following,


    addWindowFocusListener(new WindowAdapter() {
        public void windowGainedFocus(WindowEvent e) {
            System.out.println("gain 2" );
        }
        });

      

this event is not fired either.

All these jwindows have one JLabel with an image on it.

+2


source to share


5 answers


JWindow does not receive focus / window events from memory.



+3


source


To be custom, you need to create JWindow

with a parent Frame

eg new JWindow(parentFrame)

. Do this and I think you should find that it will automatically get focus when you set it to visible.



+2


source


You can call setFocusableWindowState(true)

on JWindow

so that it can be focused. But this "still" is not enough. JWindow

should also have custom components and I still can't get it to work. Using JFrame setUndecorated()

seems like the best choice.

+1


source


Not sure what you are trying to do. If you are trying to hide the popup manually, you should probably use a WindowListener and handle the windowDeactivated event.

0


source


If you really want to display the popup menu, you should use JPopupMenu without implementing it yourself.

0


source







All Articles