DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    23

    Question 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.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299

    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

  3. #3
    Join Date
    Jul 2005
    Posts
    23
    Thank you very much norm..

    Ill put that in my program + with some notes for your contribution

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links