File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Making JFormattedTextField less smart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making JFormattedTextField less smart" Watch "Making JFormattedTextField less smart" New topic

Making JFormattedTextField less smart

Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149

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 )



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,

Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
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

Joined: Apr 30, 2003
Posts: 820

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.
I agree. Here's the link:
subject: Making JFormattedTextField less smart
It's not a secret anymore!