Java Listen to Button and Keyboard click

14,349

Solution 1

Create an Action. Then you add the Action to the button by using an ActionListener. And you handle the keyboard event by binding the Action to a KeyStroke.

Read the Swing tutorial. There are section on:

  1. How to Use Actions
  2. How to Use Key Bindings

to get you started.

Or you can also just assign a mnemonic to the button to invoke the button. Read the JButton API.

Solution 2

There is one more way to achieve that what you want, simply add the commented line to your code part.

aButton = new JButton ("A");
aButton.setFont (BUTTON_TEXT);
// Doing this will also allow you to use your Keyboard for this JButton. You have to 
// press ALT+A in this case to make it work.
//aButton.setMnemonic(KeyEvent.VK_A);

If you have only one Button, that you are concerned about you can make that button the default JButton in this case too, by doing this :

yourFrameObject.getRootPane().setDefaultButton(aButton);

Now on the press of the Enter key it will do the work as described in it's actionPerformed() method.

Note : The exact implementation of the default button feature depends on the look and feel. For example, in the Windows look and feel, the default button changes to whichever button has the focus, so that pressing Enter clicks the focused button. When no button has the focus, the button you originally specified as the default button becomes the default button again.

Solution 3

Use keybinding in-order to bind the enter key with the particular component. In your case you could use

aButton.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"pressed");
aButton.getActionMap().put("pressed",btnAction);

where pressed is a keyword that links the particular component with btnAction which is a reference to an Action. An Action can be created using the AbstractAction() in which you could specify the action to be performed. if you want the same component to respond to mouse click the you could use the method

aButton.addActionListener(btnAction);

since Action is extended from ActionListener interface this won't cause a problem.

Share:
14,349
nowhere
Author by

nowhere

Updated on June 04, 2022

Comments

  • nowhere
    nowhere almost 2 years

    How to listen to key pressed and trigger a JButton?

    For example:

    I have "A" JButton as the GUI on a panel. I have a buttonListener implemented on "aButton", that will change the screen to something else. I want this button to be triggered by both mouse click and keyboard pressed.

    How can I trigger this "A" JButton by pressing "a" on keyboard while having the buttonListener implemented as well?

    My current code cannot triggered the key event.

    ButtonListener listener;
    KeyboardListener keyboardListener;
    private JButton aButton;
    
    public MyButtonPanel() {
        listener = new ButtonListener();
    
        aButton = new JButton ("A");
        aButton.setFont (BUTTON_TEXT);
        aButton.setPreferredSize (new Dimension (60,30));
        aButton.addActionListener (listener);
        aButton.addKeyListener (keyboardListener);
    
        setLayout (new BorderLayout());
        add (aButton, BorderLayout.CENTER);
    
    }
    
    private class KeyboardListener implements KeyListener
    {
        public void keyPressed(KeyEvent arg0) {
            char c = arg0.getKeyChar();
            System.out.println("Pressed " + c);
        }
    
        public void keyReleased(KeyEvent arg0) {
            char c = arg0.getKeyChar();
            System.out.println("Released " + c);
        }
    
        public void keyTyped(KeyEvent arg0) {
            char c = arg0.getKeyChar();
            System.out.println("Typed " + c);
        }
    
    }
    
     private class ButtonListener implements ActionListener {
      public void actionPerformed (ActionEvent event) {
        Object source = event.getSource();
    
        if (source == aButton) {
            System.out.println("This is a");
        }
      }
     }
    

    }