質問

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?

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top