Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a serious problem while trying to build a menu for my game. I've added two JButton to a main JPanel and added an ActionListener for each of them. The main JPanel also contains the game JPanel which have the keyPressed method inside keyController.

That's how it looks -
Main ->
      JPanel ->
        JButton, JButton,
        JPanel which contains the game and keyPressed function inside KeyController class which worked fine before I added the ActionListener for JButton.

For some reason after I added an ActionListener for each of the button, the game JPanel is not getting any keyPreseed events nor KeyRealesed.

Does anyone know the solution for my situation?
Thank you very much!

Main window -

  Scanner in = new Scanner(;

    JFrame f = new JFrame("Square V.S Circles");

    f.add(new JPanelHandler());


JPanelHandler(main JPanel) -


        JButton mybutton = new JButton("Quit");
        JButton sayhi = new JButton("Say hi");

        sayhi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)

         mybutton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        add(new Board(2));

Board KeyController(The code inside is working so it's unnecessary to put it here) -

private class KeyController extends KeyAdapter {

    public KeyController()

    public void keyPressed(KeyEvent e) {



    public void keyReleased(KeyEvent e){



share|improve this question
What do you do with your KeyController? I don't see where you use it in the code you posted. – nathan Nov 23 '12 at 12:36

The problem is that you didn't include this statement:


Where "foo" is the object ( That extends JComponent ) that can add Key Listeners, and "bar" is the class that contains the Key Listener. You can have the class implement KeyListener, or use a private inner class that extends KeyAdapter, which is really up to you.

However, event handlers are handled by AWT, not Swing itself, which may lead to some threading issues. Therefore, use Key Bindings and Action Maps, which simply add to the swing thread.

share|improve this answer

Instead of adding KeyListeners to your panel, try overriding Component.processKeyEvent in your window / frame class. You may also need to call enableEvents(AWTEvent.KEY_EVENT_MASK); in the initialization. This has generally been the most robust way of handling keyboard in Java that I am aware of.

share|improve this answer
Using is the most robust and flexible way to handle events on any components in Swing. It's a best practice to use listeners. – nathan Nov 23 '12 at 12:34
Key listeners are good for handling keyboard events on components. Instead if you are making a game and are generally interested in the keyboard events unrelated to any components, I have found this approach to work better. – msell Nov 23 '12 at 14:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.