• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programming the Enter key to function as mouse click.

 
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
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at Key binding and the KeyStroke class.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • 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 Hand
Posts: 2668
9
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic