• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Reset of JTextField data

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic