This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Button and Keyboard linking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Button and Keyboard linking" Watch "Button and Keyboard linking" New topic

Button and Keyboard linking

igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
Hello, I have a slight problem in my java swing application. I've created a JButton and added an actionListener to it within the constructor like this.

//action listener for button2
button2.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) {

do something

}//end method actionPerformed
//end of actionlistener for button1

Now, the problem I'm facing is that, I want to make the ENTER button on the keyboard to have the same effect as the button, button2. How do I do this without having to rewrite the entire actionPerformed method. It's actually quite long. Such that if the user presses button2 or presses enter, it has the same effect. Thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
wood burning stoves
subject: Button and Keyboard linking
Similar Threads
hitting the enter on the keyBoard should default to "YES" button on the html/jsp page
Please help me
Correct behaviour when launching a new window and getting rid of the first window
doing same job on two or more events
JButton action not called