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 Reset of JTextField data 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 "Reset of JTextField data" Watch "Reset of JTextField data" New topic
Author

Reset of JTextField data

ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

I have a JTextfield(with a default value) on which I am doing some validation of the text entered in it. Once the validation is done, I throw appropriate error message using Joptionpanes. After closing the pop-up, the text field should get reset to the original data set in it and not the invalid data. In my case, the textfield contains the invalid data.

Since I am using documentListener and focusListener (to invoke actions based on the data entered), once the data is entered on the field, the text value of the field changes and hence I lose the original data.

Please let me know if there is any existing API for resetting and if not, how can this be achieved.

Thanks in advance!!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by ram shyam:
I have a JTextfield(with a default value) on which I am doing some validation of the text entered in it. Once the validation is done, I throw appropriate error message using Joptionpanes. After closing the pop-up, the text field should get reset to the original data set in it and not the invalid data. In my case, the textfield contains the invalid data.

Since I am using documentListener and focusListener (to invoke actions based on the data entered), once the data is entered on the field, the text value of the field changes and hence I lose the original data.

Please let me know if there is any existing API for resetting and if not, how can this be achieved.


Is there a reason you need to use a JTextField instead of a JFormattedTextField?

A JFormattedTextField maintains both its current text and its last valid value, which is what you seem to want. (Of course it also does validation and data reversion.)


bitguru blog
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi,

Many thanks for your reply.

I changed my code to use JFormattedTextfield as suggested by you and was able to reset once the validation fails.
But I find another problem which was perfectly fine when I used JTextField.
I am using textfield.setDocument() method to additional validation on the day entered on the field which is not happening now while using the JFormattedTextfield.

Here is the code -

private static DateFormat dateformat= DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
private static NumberFormat numberformat= NumberFormat.getInstance();
private static JFormattedTextField startDt = new JFormattedTextField(dateformat);
private static JFormattedTextField endDt = new JFormattedTextField(dateformat);
private static JFormattedTextField no_of_days = new JFormattedTextField(numberformat);


startDt.setDocument(new ValidationDocument(8, FieldDefinition.DATE));
startDt.setValue(d);
startDt.setColumns(10);

endDt.setDocument(new ValidationDocument(8, FieldDefinition.DATE));
endDt.setValue(Calendar.getInstance().getTime());
endDt.setColumns(10);

no_of_days.setDocument(new ValidationDocument(3, FieldDefinition.NUMERIC));
no_of_days.setValue(new Integer("7"));
no_of_days.setColumns(5);

In the above code, ValidationDocument is the code that does some validation based on the data entered. For eg., in the no_of_days field, it does not allow to enter any alpha characters, allows only numeric data.
This validation happened perfectly when i used JTextField but not now.
Is that I am missing anything in this code??

Please clarify.

Thanks in advance!!!
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi,

Following my previous mail, I used DateFormatter and NumberFormatter instead of DateFormat and NumberFormat and using the method setAllowsInvalid(false), I was able to restrict entering invalid data on the text field for the no_of_days field, but delete and backspace are also restricted which should are valid key events.

In the case of date field, invalid data can still be entered. For eg., alpha characters should not be entered on the date field which is still occuring even after using the method mentioned above.

Please let me know whether I am missing anything.

Thanks in advance!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reset of JTextField data
 
Similar Threads
html:error to print specific error
Values are not saved in Session
Problem in Reset button.
JTextField
Struts 2 : Does s:reset is really capable for reset ?