Partial match in MaskFormatter for jFormattedTextField
- by Toto
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()