Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Programming the Enter key to function as mouse click.  RSS feed

 
Frank Mbah
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am just new in a Java class. Am try to solve an assignment. I have two JTextFields, textFieldA and textFieldB with on JButton buttonA. A lick on the button controls the action of entries on the field. How do I program the ENTER key to do same thing on the textfields as a mouse click on the button. Thanks
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at Key binding and the KeyStroke class.
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

This discussion would fit better in our GUIs forum, so I shall move you there.
 
Rob Camick
Ranch Foreman
Posts: 2818
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should be using an ActionListener on your JButton.

So, you add the same ActionListener to the text field.

The default functionality for the JTextField is to invoke its ActionListener when the Enter key is pressed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!