I try to limit the number of allowable characters to 5 by using
try {
jFormattedTextField2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("*****")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
However, when I only enter 1 character, there will always be 4 others space being padded. How can I avoid the padding, at the same time limiting number of characters?
JFormattedTextField
leads to evil UIs.
The class you want is javax.swing.text.DocumentFilter
. This allows you modify mutations in a chain-of-command style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With