Use setFormatterFactory() of JFormattedTextField to change the formatter - Java Swing

Java examples for Swing:JFormattedTextField

Introduction

To set a date format to a JFormattedTextField.

import java.text.SimpleDateFormat;

import javax.swing.JFormattedTextField;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;

public class Main {

  public static void main(String[] args) {
    DateFormatter df = new DateFormatter(new SimpleDateFormat("mm/dd/yyyy"));
    DefaultFormatterFactory dff = new DefaultFormatterFactory(df, df, df, df);
    JFormattedTextField ssnField = null;
    ssnField.setFormatterFactory(dff);
  }
}

Related Tutorials