Special Characters Used to Specify a Mask - Java Swing

Java examples for Swing:JFormattedTextField

Introduction

A mask formatter uses the special characters listed in the following table.

Character Description
# A number
? A letter
A A letter or a number
* Anything
U A letter, with lowercase characters mapped to their uppercase equivalents
L A letter, with uppercase characters mapped to their lowercase equivalents
H A hexadecimal digit (A-F, a-f, 0-9)
' A single quote. It is an escape character that is used to escape any of the special formatting characters.

To let the user enter a social security number in the ###-##-#### format.

Demo Code

import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class Main {

  public static void main(String[] args) {
    MaskFormatter ssnFormatter = null;
    JFormattedTextField ssnField = null;
    try {//w w  w.jav  a  2s  .  c  o  m
      ssnFormatter = new MaskFormatter("###-##-####");
      ssnField = new JFormattedTextField(ssnFormatter);
    } catch (ParseException e) {
      e.printStackTrace();
    }

  }
}

Related Tutorials