I have a JFrame called gameFrame and I added a Jpanel called introPanel to it, gameFrame.add(introPanel)
and I wanted to listen to the JPanel with a keylistener so I added one. When the user presses Enter, I removed the JPanel from the gameFrame and added the MainMenu theoretically, however the program doesnt listen to my keys. So i looked online and through SO and found out that I had to make the panel focusable so I did:
public IntroMenuStart() {
this.addKeyListener(this);
this.setFocusable(true);
this.requestFocusInWindow();
}
However this did not work either. What else can I do to fix this?
Each Panel is a seperate class and they all get removed from gameframe and the next panel is added.
I would prefer to do this with keylistener.
EDIT
I fixed it by including this in my code for anyone wanting to know but I'm going to be changing my code to Keybindings like the 2 answers suggsted.
public void addNotify() {
super.addNotify();
requestFocus();
}