MaskFormatter leaving empty space

0

In my project, I have an age field, where I want to receive a maximum of 3 numbers, so I did this:

mskIdade = new MaskFormatter("###");

So far so good, but every time I type a number with less than 3 characters, mskIdade fills in the remaining fields with empty spaces ( " " ). I'm pretty sure this is because% forces the field to be three characters long. How can I not force the field to be 3 characters long and accept only numbers?

    
asked by anonymous 07.06.2016 / 22:33

2 answers

6

You can use trim () in your String to remove left and right empty spaces from the value:

String str = formattedTextField.getText().trim();

Another way, maybe even better by giving you more control of what is typed, is by using the PlainDocument . With it, you not only control the number of characters you enter, but also just numbers:

class JTextFieldLimit extends PlainDocument {

    private int limit;

    JTextFieldLimit(int limit) {
        super();
        this.limit = limit;
    }

    @Override
    public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
        if (str == null) {
            return;
        }

        if ((getLength() + str.length()) <= limit) {

            super.insertString(offset, str.replaceAll("\D++", ""), attr);
        }
    }
}

Then just apply to any JTextfield :

    JTextFieldLimit limitDocument = new JTextFieldLimit(3);
    seuTextField.setDocument(limitDocument);

The signature of the insertString method receives three parameters:

  • int offset = indicates in which index of the current string of the field, the new one will be added;

  • String str = is the new string to be added (digits in your case);

  • AttributeSet attr = are attributes of the string (like type, size and font style, etc ...), in this case, it did not make any difference.

No str.replaceAll("\D++", "") , I'm passing a Regular Expression that will remove any characters passed in the string other than digits.

Remembering that the constructor of class JTextFieldLimit receives the limit of characters that its field can have, and this class can be used in any field of text.

  

Note: With the class shown above, you do not need to use    MaskFormatter and nor JTextFormatterField .

References:

Limit JTextField input to a maximum length (java2s)

How to implement in Java (JTextField class) to allow entering only digits?

Limiting the number of characters in a JTextField

    
07.06.2016 / 23:04
1

I've done two methods that can help you with this problem:

Check if there are only numbers in a String.

public static boolean apenasNumeros(String text){
    return text.matches("[0-9]+");
}

Return only the numbers of a String.

public static String tirarTudoExcetoDigitos( String text) {
       if (text == null || text.length() == 0) {
           return "";
       }
       return text.replaceAll("\D+", "");
    }

Small example:

public static void main(String[] args) {
    String test1 = "12345";
    System.out.println("Teste 1:");
    System.out.println("Apenas Números? " + apenasNumeros(test1));
    System.out.println("Apenas Números!! " + tirarTudoExcetoDigitos(test1));
    String test2 = "123A5";
    System.out.println("Teste 2:");
    System.out.println("Apenas Números? " + apenasNumeros(test2));
    System.out.println("Apenas Números!! " + tirarTudoExcetoDigitos(test2));
}

Result:

Teste 1:
Apenas Números? true
Apenas Números!! 12345
Teste 2:
Apenas Números? false
Apenas Números!! 1235
    
07.06.2016 / 23:18