Use JFormattedTextField to set a limit on the number of characters that can be entered in a field - Java Swing

Java examples for Swing:JFormattedTextField

Description

Use JFormattedTextField to set a limit on the number of characters that can be entered in a field

Demo Code

import java.text.ParseException;

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

public class Main {

  public static void main(String[] args) {
    try {// w w w .j  av a2  s .  c  o m
      JFormattedTextField twoCharField = new JFormattedTextField(
          new MaskFormatter("**"));
    } catch (ParseException e) {
      e.printStackTrace();
    }

  }
}

Related Tutorials