event listener (how to)

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

  1. wee

    wee Guest

    hello,

    i added an actionListener to a JButton. it works well when i click it with a mouse. if i put the focus on the button using the tab key and press the keyboard enter key, nothing happens. my question then is, how can i make theJButton react to both mouse click and the keyboard enter key? do i need toadd a keypressed listener on top of the actionListener? any help would be appreciated.

    thank you.
     
    wee, Aug 9, 2011
    #1
    1. Advertising

  2. wee

    markspace Guest

    On 8/8/2011 7:16 PM, wee wrote:
    > hello,
    >
    > i added an actionListener to a JButton. it works well when i click it
    > with a mouse. if i put the focus on the button using the tab key and
    > press the keyboard enter key, nothing happens. my question then is,
    > how can i make the JButton react to both mouse click and the keyboard
    > enter key? do i need to add a keypressed listener on top of the
    > actionListener? any help would be appreciated.



    I think you want to use a key binding:

    <http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html>


    package test;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;

    public class EventTest {

    public static void main( String[] args )
    {
    SwingUtilities.invokeLater( new Runnable()
    {

    public void run()
    {
    createGui();
    }
    } );
    }

    private static void createGui() {
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    JButton b = new JButton( "Test me!" );
    b.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    System.out.println( "Action: "+ e );
    }
    } );
    b.getInputMap().put( KeyStroke.getKeyStroke( "ENTER" ),
    "Enter!!!");
    Action printAction = new AbstractAction() {

    public void actionPerformed( ActionEvent e )
    {
    System.out.println( "Print Action:" );
    System.out.println( e );
    }
    };
    b.getActionMap().put( "Enter!!!", printAction );
    panel.add( b );
    frame.add( panel );

    frame.pack();
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setLocationRelativeTo( null );
    frame.setVisible( true );
    }

    }
     
    markspace, Aug 9, 2011
    #2
    1. Advertising

  3. On 8/8/2011 7:16 PM, wee wrote:
    > hello,
    >
    > i added an actionListener to a JButton. it works well when i click it
    > with a mouse. if i put the focus on the button using the tab key and
    > press the keyboard enter key, nothing happens. my question then is,
    > how can i make the JButton react to both mouse click and the keyboard
    > enter key? do i need to add a keypressed listener on top of the
    > actionListener? any help would be appreciated.
    >
    > thank you.


    Look at JRootPane.setDefaultButton().

    --

    Knute Johnson
     
    Knute Johnson, Aug 9, 2011
    #3
    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:
    841
    Mmm_moo_cows
    Oct 26, 2004
  5. Mike Scirocco
    Replies:
    4
    Views:
    293
    Mike Scirocco
    Apr 5, 2007
Loading...

Share This Page