سؤال

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