This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes JTextField question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField question" Watch "JTextField question" New topic
Author

JTextField question

Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
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

Joined: Jun 13, 2009
Posts: 2177
    
    7
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
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
Moving to our GUIs forum.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1131

Ravi,
Are you familiar with Formatted Text Field ?

Good Luck,
Avi.
Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField question