Partial match in MaskFormatter for jFormattedTextField

Posted by Toto on Stack Overflow See other posts from Stack Overflow or by Toto
Published on 2010-03-16T14:36:39Z Indexed on 2010/03/16 18:41 UTC
Read the original article Hit count: 368

Filed under:
|

How do I set up the MaskFormatter and the JFormattedTextField in order to allow partial matches?

For example:

I want the user to be able to enter numbers with this mask "## ###### ####", so if the user enters '123456789123' the formatter changes it to '12 345678 9123'.

However, I also want the user to be able to enter '12346578', be converted to '12 345678' and still be a valid value (so I can get it using jFormatedTextField1.getValue())

This is part of my code (most of it generated by Netbeans):

private javax.swing.JFormattedTextField jFormattedTextField1;
// ...
try {
    jFormattedTextField1.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("## ###### ####")));
} catch (java.text.ParseException ex) {
    ex.printStackTrace();
}

I've tried setting the focusLostBehavior to PERSIST so the value is not cleard after exiting the jFormattedTextField1, but that way I get a null when using the calling jFormattedTextField1.getValue()

© Stack Overflow or respective owner

Related posts about java

Related posts about swing