Java tutorial
import java.awt.BorderLayout; import java.text.ParseException; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; public class MainClass { public static JPanel demo1() { JPanel pan = new JPanel(new BorderLayout()); pan.setBorder(new TitledBorder("Demo 1: format toggles with focus")); MaskFormatter withFocus = null, withoutFocus = null; try { withFocus = new MaskFormatter("LLLL"); withoutFocus = new MaskFormatter("UUUU"); } catch (ParseException pe) { } DefaultFormatterFactory factory = new DefaultFormatterFactory(withoutFocus, null, withFocus); JFormattedTextField field = new JFormattedTextField(factory); field.setValue("Four"); pan.add(field, BorderLayout.CENTER); return pan; } public static void main(String argv[]) { JFrame f = new JFrame("FactoryDemo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(demo1(), BorderLayout.NORTH); f.getContentPane().add(new JTextField(), BorderLayout.SOUTH); f.setSize(240, 160); f.setVisible(true); } }