aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes setting keyboard navigation (shortcut keys) to the Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setting keyboard navigation (shortcut keys) to the Buttons" Watch "setting keyboard navigation (shortcut keys) to the Buttons" New topic
Author

setting keyboard navigation (shortcut keys) to the Buttons

raj lakshmi
Greenhorn

Joined: Sep 03, 2003
Posts: 7
Hi,
I need to set hot keys / shortcut keys /mnemonics whatever u call...to the objects of class Button.
I found an option to set Mnemonics to an instance of JButton but plz let me know a way to create shrt keys to a plain java.awt.Button class without using any event handling mechanisms or so ..
Tell me is it possible first of all without using any event handling mechanisms?

thanks in advance.
Shan Accent
Greenhorn

Joined: May 08, 2005
Posts: 23
AWT's Button doesn't have a method for this, the only solution I can think of is adding a keylistener to the button.

Button btn = new Button("Press Me");
btn.addKeyListener(myKeyListener);
raj lakshmi
Greenhorn

Joined: Sep 03, 2003
Posts: 7
hi raj thank you for your response...can u plz detail of how to handle the shortcut keys after adding this listener.
hope I am nt bugging fr a minute thing!! very new to awt
Shan Accent
Greenhorn

Joined: May 08, 2005
Posts: 23
create the myKeyListener class and implement KeyListener interface then override the following interface methods.

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("keyPPressed");
//Get the key code and process what key you want.
}

public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}

public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}


Note:
It works only when it focused on the particular button or key listener component.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's one way



but the better method (apparently) is to use KeyBindings
http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
raj lakshmi
Greenhorn

Joined: Sep 03, 2003
Posts: 7
Hi thanks fr ur replies...I could complete the task with the tips given by you people.

thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting keyboard navigation (shortcut keys) to the Buttons