DIGIT-ARC read-only archive · snapshot: July 23, 2026

Adding Keyboard Event to JButton

8 posts · originally from https://geek.digit.in/community/threads/adding-keyboard-event-to-jbutton.93610/

abhinav_bipnesh
abhinav_bipnesh Original post #1
Jul 21, 2008 at 11:18 AM
Hi Guy,

I have started learning Swing programming in java and for that i am creating a sample calculator application. For this i have made the JtextField a non focus object so when i click any of JButton textField take the input without any problem. Now i want to add keyboard event to these button so that when i press any numeric key on keyboard the textFiekd have the input for it.

Say for example if I press Esc key then all the text in textField get clear or when I press Num 7 key then textField has text as 7. But I am not able to add key event to the JButton. So please tell me how to do so.

Any help will be appricated.

Thanks in advance.
C
Jul 21, 2008 at 12:48 PM
Create a KeyListener instance which does the handling for keys. Here you can check the key pressed and append the appropriate character to JTextField.

Then add this KeyListener instance to all the JButton instances which should handle the event using the addKeyListener() method.

If you want all the components of the container to act in the same fashion you can use this method *java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponents() to retrieve all components and add the listener to all of them by iterating through them.
ray|raven
Jul 21, 2008 at 1:41 PM
^Or , you could add a Mnemonic using JButton.setMnemonic

It works with the KeyCodes mentioned here : *java.sun.com/javase/6/docs/api/java/awt/event/KeyEvent.html
C
Jul 21, 2008 at 2:33 PM
^Or , you could add a Mnemonic using JButton.setMnemonic

Mnemonics may not work out for him as mnemonic means hitting Alt+<some_key>. But he wanted to just press 7 on JButton and get it in the Textfield, I guess somewhat like how various calculator apps behave.
ray|raven
Jul 21, 2008 at 2:37 PM
^Ah. Missed that part. Thanx for correcting me. :)
abhinav_bipnesh
Jul 21, 2008 at 4:00 PM
thanks guys,

But can you provide me a small code snapshot so that i can get some more clear picture.
C
Jul 21, 2008 at 11:26 PM
Try this code. It is a simple one and may not be really well-written but should give you an idea.
Code:
package demo;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class DemoFrame extends JFrame {
    private JTextField textField;
    private JButton button1, button2;

    public DemoFrame() {
        textField = new JTextField(10);
        button1 = new JButton("1");
        button2 = new JButton("2");

        setSize(250, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout());
        textField.setEditable(false);

        getContentPane().add(textField);
        getContentPane().add(button1);
        getContentPane().add(button2);

        KeyListener listener = new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();

                switch (keyChar) {
                case KeyEvent.VK_1:
                    textField.setText(textField.getText() + "1");
                    break;
                case KeyEvent.VK_2:
                    textField.setText(textField.getText() + "2");
                    break;
                case KeyEvent.VK_ESCAPE:
                    textField.setText("");
                }
            }
        };
        
        Component [] components = getContentPane().getComponents();
        for(Component component : components)
            component.addKeyListener(listener);
    }

    public static void main(String[] args) {
        new DemoFrame().setVisible(true);
    }
}
abhinav_bipnesh
Jul 22, 2008 at 10:32 AM
thanks man i will try it and this will help me out.......... :)