Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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(System.in);

    JFrame f = new JFrame("Square V.S Circles");
    f.setUndecorated(true);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    f.add(new JPanelHandler());
    f.pack();

    f.setVisible(true);
    f.setLocationRelativeTo(null);

JPanelHandler(main JPanel) -

  super.setFocusable(true);


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

        sayhi.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Hi");
        }
        });      

         mybutton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e)
        {
           System.exit(0);
        }
        });      
        add(mybutton);
        add(sayhi);
        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()
    {
        ..Code
    }


    @Override
    public void keyPressed(KeyEvent e) {

    ...Code

    }

    @Override
    public void keyReleased(KeyEvent e){

     ...Code

    }


}
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

1 Answer 1

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

 
discard

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.