Question

I have a jwindow(set to be always on top) that you can click to get a pop menu. If the user right clicks the window it shows the pop menu but then if the user clicks any other window(such as firefox) pop menu does not disappear.

I tried to fix it by adding FocusListener on the 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 there event never get called. i also tried the following,


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

this event also not called.

All this jwindows has is a single JLabel with a picture on it.

Was it helpful?

Solution

From memory JWindow's do not receive focus/window events.

OTHER TIPS

You are suppose to call setFocusableWindowState(true) on a JWindow to allow it to be focusable. But that "still" is not enough. The JWindow must also have focusable Components and I'm still not able to get it to work. Using JFrame setUndecorated() seems the best choice.

To be focusable, a JWindow needs to be created with a parent Frame, like new JWindow(parentFrame). Do that and I think you should find it will automatically get the focus when you set it to visible.

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

If you really want to display a popup menu, you should be using JPopupMenu, not implementing it yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top