0

Okay I would like to set a Mnemonic for a JButton so that if I press the right arrow key the button will be activated. However I can not find what the command for that would be. Right now I have it so that you can hold down alt+Right to activate the button (setMnemonic(KeyEvent.VK_RIGHT)) but I want to make it so that I can just hit the right arrow with no "alt" necessary.

EDIT: I realize I spelled 'seting' wrong :$ I was in a hurry please don't harass me for it

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by TheWhite
0

Mnemonics uses the mouseless modifier which is alt by default.

instead, u can use a KeyListener and get the keycode inside keypressed and match it to KeyEvent.VK_RIGHT. from there, u can do button.doClick() to do the action.

JButton b = new JButton("test");
b.addKeyListener(new KeyListener() {
	public void keyPressed(KeyEvent ke) { 
		if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
			b.doClick(); 
	}
        .....
});

If you have multiple buttons, it may be better to have your class implement KeyListener and have ur button, simply addKeyListener(this) and do one of these:

public void keyPressed(KeyEvent ke) {
	if ((ke.getComponent() == b) && (ke.getKeyCode() == KeyEvent.VK_RIGHT))
		b.doClick();		
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.