Formatting text Fields

I want to have text fields formatted in the following way:

date YYYY-MM-DD

phone (XXX) 555-5555

I know that I can have for example:

new JFormattedTextField(newnew simpleDateFormat("yyyy-MM-dd"));

But I want the dashes to be permanently in the text field and as a user types the date the prompt moves over to the next spot automatically. The same goes for the phone number .

So for example: Initially I see "- - " Then I start typing:

"200 - - " -> "2007-0 - ". I guess I've been clear enough.

There is a lot of forms like this on the internet

[726 byte] By [aurir_a] at [2007-11-26 18:07:14]
# 1

Nevermind, I got it:

javax.swing.text.MaskFormatter fmt = null;

// A phone number

try {

fmt = new javax.swing.text.MaskFormatter("###-###-####");

} catch (java.text.ParseException e) {

}

ftfPhone = new JFormattedTextField(fmt);

aurir_a at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 2

The formatting works really nice BUT:

I have a "reset" button which should reset the whole form. It calles a reset() which uses

tftSSNum.setText("");

It does clear the form, but if I click on SS# field and then click on any other it reverts the previous value. No doubt, it can contribute to many errors. How do I clear JFormattedTextFileds so that values entered are gone forever?

aurir_a at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 3
tftSSNum.setValue("");
camickra at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 4
I haven't triedsetValue(""): yet butsetValue(null); works as well. I just figured it out.
aurir_a at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 5
Supposedly I hava a textField ss# ### - ## - #### and i would like to retrieve the value as a number not a string. How do i go about it?
aurir_a at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 6
You can retrieve it as a String and convert it to a number via Integer.parseInt(String) or new Integer(String).Of course, any leading zeros will be lost.
JayDSa at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 7
Ok, so the retrieved string is "555-555-5555" and because of the dashes I get NumberFormatException. I used:int phone = Integer.parseInt(ftfPhone.getText());But is there anything I could do withtftPhone.getValue();?
aurir_a at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 8
a) use String.split to get the indivdual numbersb) loop through the array and combine each number into one stringc) use Integer.parseInt(...) on the string from b.
camickra at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 9
Try MaskFormatter.setValueContainsLiteralCharacters(false);
JayDSa at 2007-7-9 5:38:29 > top of Java-index,Desktop,Core GUI APIs...