Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting keyboard navigation (shortcut keys) to the Buttons

 
raj lakshmi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks fr ur replies...I could complete the task with the tips given by you people.

thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic