how do I limit user to put certain format in JTextField

I want to create a JTextField which will limit user to put date format only like (##/##/####) a date format. how do I do it? thanks
[138 byte] By [aboostera] at [2007-11-26 20:18:18]
# 1
JFormattedTextField?API: http://java.sun.com/javase/6/docs/api/javax/swing/JFormattedTextField.htmlDevelopers Almanac: http://www.exampledepot.com/egs/javax.swing.text/formtext_FormTextDate.html
pbrockway2a at 2007-7-10 0:41:51 > top of Java-index,Java Essentials,New To Java...
# 2

boosta,

Are you limited to a text based interface? I mean why not a visual calendar control?

I just googled "java calendar control" and came up with http://builder.com.com/5100-6370-1045263.html and http://www.toedter.com/en/jcalendar/ on the first page...

If you must use a JTextField you'll need to use EventListener(s) to filter keyboard input... I've never tried it... I've never had to.... and if this is not specific stated requirement then I'd drop it like a gun.

I (like many other users) hate fancy custom interfaces which don't behave as you expect... and I (like many other users) am just to lazy to RTFM.

keith.

corlettka at 2007-7-10 0:41:51 > top of Java-index,Java Essentials,New To Java...
# 3

If you're going to be looking at what the user keys into the JTextField then you should be using a DocumentListener and not mucking about looking at keystrokes. The DocumentListener abstracts out the keystrokes (including paste and backspace and so on) and just shows you what changed.

However you can't edit a date by looking at the keystrokes. You can't tell whether a string is a valid date until you have the entire string. For example if the user has keyed in "02/29/201" then that will be valid if the next character input is "2" or "6" but invalid otherwise.

DrClapa at 2007-7-10 0:41:51 > top of Java-index,Java Essentials,New To Java...
# 4
corlettk,Coooool!!! I was thinking that I only can use JTextField. However, the one you post is exectly what I want. Thank you very much.
aboostera at 2007-7-10 0:41:51 > top of Java-index,Java Essentials,New To Java...