Results 1 to 3 of 3
  1. #1
    adeeb is offline Member
    Join Date
    May 2008
    Rep Power

    Default Regarding Listener

    i have a JButton i want to add it the enter key listener i.e when i press enter key then it should perform a task.
    Please tell me how can i acheive this.
    Thanks and Regards.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    One way is to set the button as the default button for your JFrame or JDialog or JApplet via the JRootPane method: setDefaultButton(...)

    JRootPane (Java Platform SE 6)

  3. #3
    Stephen is offline Member
    Join Date
    Jun 2008
    Rep Power


    You can also add a keylistener to the button.

    You have to import java.awt.event.*;

    Set focus to button with JButton.requestFocus();

    Code for keylistener normally in the constructor

    JButton.addKeyListener( new KeyAdaptor()
    { public void keyPressed(KeyEvent e)
    { int key = e.getKeyCode();
    if (key == KeyEvent.VK_ENTER)
    { statements or call method }
    } );

Similar Threads

  1. add an undo listener to a Jtable
    By christina in forum Advanced Java
    Replies: 12
    Last Post: 01-29-2009, 08:47 AM
  2. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM
  3. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  4. Listener collision on game
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:48 AM
  5. Adding listener to non-Java object?
    By cruxblack in forum Advanced Java
    Replies: 5
    Last Post: 07-30-2007, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts