Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes masking dates in text fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "masking dates in text fields" Watch "masking dates in text fields" New topic
Author

masking dates in text fields

Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Hi,

I'm using a JFormattedTextField and would like to ensure that users can only enter valid dates in the format "dd/MM/yy". My first attempt at achieving this looks like this:

JFormattedTextField dateField = new JFormattedTextField(MaskFormatter("##/##/##"));

However this doesn't quite work because users can enter (for example) "44/13/07". Is there any support in the JDK for masking date input fields?

Thanks in advance,
Dan


SCJP, SCJD, SCWCD
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Dan Murphy:
I'm using a JFormattedTextField and would like to ensure that users can only enter valid dates in the format "dd/MM/yy". My first attempt at achieving this looks like this:

JFormattedTextField dateField = new JFormattedTextField(MaskFormatter("##/##/##"));

However this doesn't quite work because users can enter (for example) "44/13/07". Is there any support in the JDK for masking date input fields?


Have you tried

JFormattedTextField dateField = new JFormattedTextField(new DateFormatter(/*...*/));

It was designed for dates, while MaskFormatter wasn't.

It will allow the user to enter something like October 33rd,
but it will change it to November 2nd.


bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: masking dates in text fields
 
Similar Threads
JFormattedTextField problem
Number of charecters in JTextField
Making JFormattedTextField less smart
Use mask formatter in conjunction with a DateFormatter in Swing
How to make JFormattedTextField to show the enforced format when user is entering the content..