Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making JFormattedTextField less smart

 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I use a JFormattedTextField to limit date entries to valid
dates. Works beautiful - in fact a little too good.

My users complain that the filed changes an entry of

32.3.2005 ( 32nd of March 2005 )

to

1.4.2005


This may be extremely cool if your brain works like ours but
the average accountant would prefer an error messsage.

How can i throw an exception when this silent change is made.

Thank you,

J.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This behaviour is caused by the underlying Calendar which is managed by the DateFormat instance you are using. It allows the behaviour you describe if it is set to 'lenient' mode.

Try creating a DateFormat instance and disabling leniency, then passing this to your text field. Something like this (not tested since I don't have the JDK accessible atm):
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jefferey, thanks for asking the question and Stuart, thanks for answering it. I was scratching my head over this.

There still needs to be a way to alert the user when the date field reverts, losing the edit they have made. The property change listener does not fire if the value reverts, so you can use this fact to alert the user.

or... you can use the Input Verifier given in the docs for JFormattedTextField to return a true or false that you can use to determine if the date format accepted the date or not.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic