Creating a Text Field to Display and Edit a Phone Number - Java Swing

Java examples for Swing:JTextField

Description

Creating a Text Field to Display and Edit a Phone Number

Demo Code

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

public class Main {
  public static void main(String[] args) {
    MaskFormatter fmt = null;//from  w  ww  .  jav  a2  s  .c  o m

    // A phone number
    try {
      fmt = new MaskFormatter("###-###-####");
    } catch (java.text.ParseException e) {
    }
    JFormattedTextField tft1 = new JFormattedTextField(fmt);

    // A social security number
    try {
      fmt = new MaskFormatter("###-##-####");
    } catch (java.text.ParseException e) {
    }
    JFormattedTextField tft2 = new JFormattedTextField(fmt);
    // A social security number
    fmt.setPlaceholderCharacter('*');
    JFormattedTextField tft3 = new JFormattedTextField(fmt);
  }

}

Related Tutorials