Question

I have KeyListener in my CardLayout.

KeyListener listener = new MyKeyListener();
addKeyListener(listener);
setFocusable(true);

And it works just fine. The problem is when I exit this JPanel from my main screen manager:

private CardLayout cl;
cl.show(cardPanel, "1");

and comes back to it the KeyListener stops working. How can I evoke the KeyListener again, when the JPanel is shown?

Was it helpful?

Solution

KeyListeners have issues with focus, while you have set the component focusable, you've not requestFocusInWindow when the view has changed back.

Instead, use Key Bindings which have greater focus handling control

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