event listener

Discussion in 'Java' started by wee, Aug 9, 2011.

  1. wee

    wee Guest

    hi,

    i have a JButton object and i added an actionListener to it. it works
    well with a mouse click, but if i use the enter key while the focus is
    on the JButton, nothing happens. do i need to add a keypress listener
    on top of the actionListener to the JButton so that it would work both
    for a keypress and a mouse click? help on this query is greatly
    appreciated.

    thanks..
     
    wee, Aug 9, 2011
    #1
    1. Advertising

  2. In article
    <>,
    wee <> wrote:

    > I have a JButton object and I added an actionListener to it. It works
    > well with a mouse click, but if I use the enter key while the focus
    > is on the JButton, nothing happens. Do I need to add a keypress
    > listener on top of the actionListener to the JButton so that it would
    > work both for a keypress and a mouse click? Help on this query is
    > greatly appreciated.


    The default key binding for Swing buttons is the space key [1]. You can
    add a binding to the enter key, as shown here [2], but the root pane's
    setDefaultButton() method [3] is a convenient alternative.

    [1]<http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html>
    [2]<http://sites.google.com/site/drjohnbmatthews/keypad-panel>
    [3]<http://download.oracle.com/javase/6/docs/api/javax/swing/JRootPane.html>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Aug 9, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sasha Borodin
    Replies:
    0
    Views:
    3,099
    Sasha Borodin
    Jul 28, 2003
  2. Slobodan C

    Event listener list implementation

    Slobodan C, Jan 22, 2004, in forum: Java
    Replies:
    2
    Views:
    1,199
    Slobodan C
    Jan 23, 2004
  3. Shrish
    Replies:
    1
    Views:
    441
    Thomas Smith
    Oct 16, 2004
  4. Mmm_moo_cows

    Java Event Listener integer problem.

    Mmm_moo_cows, Oct 24, 2004, in forum: Java
    Replies:
    4
    Views:
    842
    Mmm_moo_cows
    Oct 26, 2004
  5. Mike Scirocco
    Replies:
    4
    Views:
    293
    Mike Scirocco
    Apr 5, 2007
Loading...

Share This Page