Reputation: 235
I have two JPanels. In the first one I have 3 JButtons and the second one is to draw images based on events read from the keyboard. If I set the JButtons with setEnabled(false);
I can use the keyboard events as I expect (If I press up arrow the image moves up), but when the buttons are enabled nothing happens with the image. Even, if I press the space bar it behaves like if I clicked the button.
Upvotes: 1
Views: 529
Reputation: 285405
The problem is not with the JButtons, but is likely because your using a KeyListener. Don't use KeyListeners with Swing GUI's if you can avoid it and instead use Key Bindings. KeyListeners only work if the component being listened to has the focus, and when you have JButtons present, they will take the focus and prevent your KeyListener from working. Key Bindings, if done right, can avoid this problem.
For example, please see my code example here.
Upvotes: 5