Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField question

 
Ravi Shankarappa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a JTextField accept values only when "Enter" on keyboard is hit. That is if you type something and move the mouse away before hitting return, then it should put back the old value. How to force this behavior in jTetxtField? The purpose of this is that then I can do some verification of the text data entry.

Thanks in advance.

-Ravi
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Add a FocusListener to the JTextField. On focusGained you save the current value. On focusLost you reset the text field with the saved value.

2) Add an ActionListener to the text field. When the ActionEvent fires you get the current value and update the saved valued.

However this sounds like a strange requirement. Maybe you should be displaying a JOptionPane asking the user to enter a value. Then if the user hits OK on the option pane you save the value otherwise you ignore the value.
 
Campbell Ritchie
Marshal
Posts: 52663
121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUIs forum.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi,
Are you familiar with Formatted Text Field ?

Good Luck,
Avi.
 
Ravi Shankarappa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avi Abrami wrote:Ravi,
Are you familiar with Formatted Text Field ?

Good Luck,
Avi.

Actually, I wasn't. Thanks for the heads up!.
And thanks to Rob too.
-Ravi
 
Your mother was a hamster and your father was a tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!