Basically, I have a panel containing a bunch of buttons. I'm trying to add some keylisteners to the buttons and/or the panel so that when I type the corresponding keys on the keyboard, the buttons will be clicked.

Any idea how to to do this?

Thanks,

Do you just want hot keys for some buttons, which require you to hold down the Alt key to access? Or do you need them to respond directly to key typed events? You can set mnemonic hot keys with setMnemonic() on each button.

If they have to respond to keys being typed, you could create a Map to associate the key characters to the button instances and call doClick() on the buttons

addKeyListener(new KeyListener() {

    public void keyTyped(KeyEvent e) {
        JButton typedButton = buttonMap.get(String.valueOf(e.getKeyChar()).toUpperCase());
        if (typedButton!=null){
            typedButton.doClick();
        }
    }
...
}

Do you just want hot keys for some buttons, which require you to hold down the Alt key to access? Or do you need them to respond directly to key typed events? You can set mnemonic hot keys with setMnemonic() on each button.

If they have to respond to keys being typed, you could create a Map to associate the key characters to the button instances and call doClick() on the buttons

addKeyListener(new KeyListener() {

    public void keyTyped(KeyEvent e) {
        JButton typedButton = buttonMap.get(String.valueOf(e.getKeyChar()).toUpperCase());
        if (typedButton!=null){
            typedButton.doClick();
        }
    }
...
}

Thanks for the reply! But well, I honestly dont know much about creating a key map, not much less what a key map is. Is there any simpler way then?
I tried something like this.

button0.addKeyListener( new KeyListener (){
public void keytyped (KeyEvent e) {
if (e.getKeyChar() == '0') button0.doClick();
}
});

But it didn't seem to work. So what do you think?

I was just referring to a hashmap that mapped the string key values to the JButtons, like so

Map<String,JButton> buttonMap = new HashMap<String, JButton>();
 ...
buttonMap.put("A",btnA);
buttonMap.put("B",btnB);
buttonMap.put("C",btnC);
buttonMap.put("D",btnD);

The key listener needs to be added to the component that will have the focus, such as the main panel or frame. Adding the listeners to the buttons themselves won't work because the buttons won't have the focus to receive the events.

Key bindings could also be used here.
http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
You would have to create a binding for each key event to the appropriate action. The benefit of key bindings would be that they don't have the same focus issues that key listeners do.

I was just referring to a hashmap that mapped the string key values to the JButtons, like so

Map<String,JButton> buttonMap = new HashMap<String, JButton>();
 ...
buttonMap.put("A",btnA);
buttonMap.put("B",btnB);
buttonMap.put("C",btnC);
buttonMap.put("D",btnD);

The key listener needs to be added to the component that will have the focus, such as the main panel or frame. Adding the listeners to the buttons themselves won't work because the buttons won't have the focus to receive the events.

Key bindings could also be used here.
http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
You would have to create a binding for each key event to the appropriate action. The benefit of key bindings would be that they don't have the same focus issues that key listeners do.

I just tried adding the KeyListener to the main frame. But the listeners sometimes don't respond. Do you happen to know what are the possible reasons for this?

P.S: Actually, it looked like once the frame lost focus, it couldn't get it back anymore. When I first run my program, the listeners worked just fine, but after I minimized the frame, they diddn't respond anymore. Could that be the problem?

Member Avatar for ztini

Post your full code, let's have a look.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.