Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > event listener (how to)

Reply
Thread Tools

event listener (how to)

 
 
wee
Guest
Posts: n/a
 
      08-09-2011
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.
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      08-09-2011
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 );
}

}


 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      08-09-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
having trouble with event listener: detect iframe close event fromparent Mike Scirocco Javascript 4 04-05-2007 03:19 AM
Java Event Listener integer problem. Mmm_moo_cows Java 4 10-26-2004 03:26 PM
event listener requesting a thread Shrish Java 1 10-16-2004 09:10 AM
Event listener list implementation Slobodan C Java 2 01-23-2004 09:00 PM
SERVLETS: extending j_security_check - filter or event listener? Sasha Borodin Java 0 07-28-2003 08:22 PM



Advertisments