Apply a mask to String : JFromattedField MaskFormatter « Swing « Java Tutorial






import javax.swing.text.MaskFormatter;

public class Main {
  

  public static void main(String args[]) throws Exception {

    MaskFormatter mf = new MaskFormatter("A-AAAA-AAAA-A");
    mf.setValueContainsLiteralCharacters(false);
    System.out.println(mf.valueToString("123123123123"));
  }
}
//1-2312-3123-1








14.19.JFromattedField MaskFormatter
14.19.1.Input Masks Summary Table
14.19.2.Formatted Masked Input: new MaskFormatter(###-##-####)Formatted Masked Input: new MaskFormatter(###-##-####)
14.19.3.Formatted Masked Input: new MaskFormatter((###)###-####) (For phone number)Formatted Masked Input: new MaskFormatter((###)###-####) (For phone number)
14.19.4.Regex Formatter with a JFormattedTextFieldRegex Formatter with a JFormattedTextField
14.19.5.Work with DefaultFormatterFactory
14.19.6.Apply a mask to String