How to format field for RG with issuing body?

3

I have the following java class:

public class TesteRG {

public static void main(String[] args) {
    String  RG = "24.77.195 ssp/pb";

    Pattern pattern = Pattern.compile("\d{2}.\d{2}.\d{3}\s\w{3}/\w{2}");
    Matcher matcher = pattern.matcher(RG);

    if (matcher.find()) {
        System.out.println("Valido");
    } else {
        System.out.println("Não Valido");
    }
}

It works, but when I call in the frame it has a jTextField named jTFRG with the following custom code "here is the problem" :

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

It just lets me enter numbers. How do I insert a String equal to: 12.123.44 SSP / SP

The validation part is working now just need to insert the letters beyond the numbers.

When I try to insert the formatted field, it only lets me put numbers and the value of the String is: 11.11.111 111/11

    
asked by anonymous 27.03.2016 / 19:08

1 answer

4

According to the documentation for the class MaskFormatter , the # character indicates the entry of only numbers, if you want to merge the numeric mask including letters, the valid formatting characters are U (turns typed letters) and L (turns typed letters into lower case letters) :

Try the below:

 MaskFormatter formatter = new MaskFormatter("##.##.### UUU/UU");
 formatter.install(jTFRG);

I tested with this mask and see the result:

Only accept numbers in the first 7 digits, then only accept letters and convert to uppercase.

    
27.03.2016 / 19:32