문제

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