tags:

views:

12

answers:

1

Hi everyone, java noob here. In my Application class, I have a JPanel with focusable set to true and a KeyListener class added to it. I also have a Timer registered with a TimerListener class set up in the Application's main function. The KeyEvents work, but once I press the JButton to call timer.start(), KeyEvents stop firing and only the timer's actionPerformed executes. When timer.stop() gets called, the KeyEvents still don't work. I know I've set this up wrong, but I can't figure out how to fix it. Can anyone help?

A: 

but once I press the JButton to call timer.start(), KeyEvents stop firing and only the timer's actionPerformed executes.

KeyEvents are only passed to the component with focus.

If you click on the button, then it has focus, not the panel.

Try resetting focus back on the panel in the button ActionListener code.

Or a better solution is to use Key Bindings. You can set up the key bindings so that the Action will still be invoked when the focus is on the panel or another component on the panel.

camickr