Adding Keyboard Event to JButton

Status
Not open for further replies.

abhinav_bipnesh

Journeyman
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.
 

chandru.in

In the zone
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

Think Zen.
^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
 

chandru.in

In the zone
^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.
 

chandru.in

In the zone
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);
    }
}
 
Status
Not open for further replies.
Top Bottom