Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of characters in JTextField

Tags:

java

swing

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?

like image 480
Cheok Yan Cheng Avatar asked Dec 29 '22 08:12

Cheok Yan Cheng


1 Answers

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.

like image 55
Tom Hawtin - tackline Avatar answered Dec 31 '22 13:12

Tom Hawtin - tackline