Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Format Mask problems - whoah! what happend to my zip code!?!??!?!?!

843789Jul 23 2009 — edited Jul 24 2009
Hi Everyone,

I am having a problem with masks that I am hoping someone can help me with. Of all the masks that I use, it seems as if only this particular one causes me grief:


Environment

Out of the box Java SE 6. No 3rd party products. Using eclipse 3.4.2 IDE.


Situation

I have a GUI that incorporates several zip codes. In an effort to programmatically ensure the integrity of the data entered, I use an input mask for these fields. The actual mask consists of #####-####,


The problem

If you do not use the zip code extension (last 4 digits of the mask), the entry disappears when you tab off of the field and navigate to the next field on the screen. For example: the user types in '70345' and hits the tab key. The entire entry disappears and the cursor and focus moves to the next field on the screen. On the other hand, if the user types in '70345-0000', the mask works as expected.

Not knowing any better, I would say, based on my observations, that you cannot use a partial mask? If you utilize the entire mask, the entry is perfect. My guess is that if I separated the two - the zip from the extension - into two fields with independent masks, I would not have a problem. I would like to stick with the single field if I could, however.

Any advice you have to offer would be greatly appreciated.

Thank You -

Ricky


One of the zip code fields

hsZip = new JFormattedTextField(createMask(ZIP_MASK));
getContentPane().add(getHsZip());
hsZip.setBounds(730, 772, 103, 28);


The method that creates and returns the appropriate mask.

public static MaskFormatter createMask( int type) throws SQLException {
MaskFormatter format = null;

try {
switch (type) {
case 1: format = new MaskFormatter("##/##/##"); // date
break;
case 2: format = new MaskFormatter("###-###-####"); // phone
break;
case 3: format = new MaskFormatter("#####-####"); // zip
break;
case 4: format = new MaskFormatter("###-##-####"); // ssn
break;
case 5: format = new MaskFormatter("####.##"); // money
break;
default:
break;
}
}
catch (java.text.ParseException exc) {
try {
showErrMsg("formatter is bad: " + exc.getMessage(), true);
}
catch (Exception e) {logException(e,true,true); }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 21 2009
Added on Jul 23 2009
2 comments
432 views