Java JFormattedTextField from MaskFormatter

Introduction

Characters you can use in the formatting mask:

Character Description
#Any valid number (Character.isDigit).
' Escape character, used to escape any of the special formatting characters.
U Any character (Character.isLetter). All lowercase letters are mapped to uppercase.
L Any character (Character.isLetter). All uppercase letters are mapped to lowercase.
AAny character or number (Character.isLetter or Character.isDigit).
? Any character (Character.isLetter).
* Anything.
HAny hex character (0-9, a-f or A-F).
import java.awt.BorderLayout;

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

public class Main {
   public static void main(final String args[]) throws Exception {
      JFrame frame = new JFrame("Formatted Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      MaskFormatter mask = new MaskFormatter("###-###-####");

      JFormattedTextField input = new JFormattedTextField(mask);

      frame.add(input, BorderLayout.NORTH);

      frame.add(new JTextField(), BorderLayout.SOUTH);
      frame.setSize(250, 100);//from   ww w  .  j  a v a  2 s. c  o  m
      frame.setVisible(true);
   }
}



PreviousNext

Related