-
button and "keyboard ENTER key"
Hi,
I have a dialog box with few buttons. How do I make a button "clicked" whenever it get focussed and I press "Enter" on the keyboard? Currently I have to use mouse and click on the button.
Thank you.
-
Class to make Enter pressed as an ActionEvent
I assume that you want the actionPerformed() method to be called when you press Enter when the button has the focus.
Here's a class that does that. Add it as a KeyListener for the button:
button.addKeyListener(new MakeEnterDoAction());
public class MakeEnterDoAction extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
Object src = ke.getSource();
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
new ActionEvent(src,
ActionEvent.ACTION_PERFORMED,
"Enter"));
}
} // end keyPressed()
} // end class
Norm
-
Thank you very much norm..
Ill put that in my program + with some notes for your contribution
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks