JPanel doesn't response to KeyListener event

Viewed 30670

I have a subclass of JFrame that uses a class extended from JPanel

public class HelloWorld extends JPanel implements KeyListener

I add an object of HelloWorld to the frame - app.add(helloWorld);. Now, when I press any keyboard key non of the KeyListener methods gets called and it seems that helloWorld doesn't have window focus. I have tried also to invoke helloWorld.requestFocusInWindow(); but still doesn't respond.

How can I make it respond to key press?

4 Answers

add this in MyFrame method;

this.addKeyListener(helloWorld);
Related