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

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();		
}
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.