I'm having trouble with the default Java KeyListener in my project. I noticed that the KeyListener doesn't seem to get KeyEvents forwarded sometimes when I start.
Symptoms of the problem: When starting the application key input isn't processed. This only happens sometimes. Sometimes I have to close and start the app 7-8 times until this shows up. Sometimes it's the first try. When it happens it won't work until I restart the app again.
What I'm using: Window 7 x64 and the newest Eclipse and JDK versions.
What I found out already: I've put a breakpoint in debug mode and checked the JPanel instance out. The KeyListener is always successfully added to it, it seems. Also, MouseListener and MouseMotionListener work just fine, all the time.
Minimal code:
public class Player implements KeyListener
{
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){ }
public void keyPressed(KeyEvent e){
System.out.println("Key Pressed!");
}
}
public class Game {
public static void main(String[] args) {
new Game();
}
public Game(){
JFrame window = new JFrame();
window.setVisible(true);
//Now set the window size correctly
window.setSize(800, 600);
//Set-up the rest of the window
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setResizable(true);
//Create our panel
JPanel canvas = new JPanel();
canvas.setFocusable(true);
window.add( canvas ); //Add it to our window
Player k = new Player();
canvas.addKeyListener(k);
}
}
Thank you for your time!
PS: Ok, answer to my own question:
It seems that I have to call setVisible(true) after setting the window's size:
JFrame window = new JFrame();
Now set the window size correctly
window.setSize(800, 600);
window.setVisible(true);
Switching out the setSize() and setVisible() like this seems to make it work. Tried it about a dozen times without a problem.
I guess setVisible might not like giving Focus to the window if it's of size 0x0. The question is: Why does this only cause a problem in one out of some cases?
Try adding a JButton to your "canvas" JPanel, then pressing the button and seeing what happens to your KeyListener -- it fails because the JPanel lost the focus. To prevent this from happening, use Key Bindings instead (see the link in my comment above for the tutorial). For e.g.,
Don't know if this is related to your problems, but due to the intermittent nature of it perhaps it is...You should execute setVisible() last and in the swing thread. You could call setSize after setVisible if you want to, but the user might see a flicker and it likewise should be done in the swing thread. Do this as your last step:
To do this, you will also need to make window declaration final: