Write the formatting mask with Characters you can use in Java

Description

The following code shows how to write the formatting mask with Characters you can use.

Example


//  ww  w  .  j  a va2 s. co  m

import java.awt.FlowLayout;
import java.text.ParseException;

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

public class Main {
  public static void main(String args[]) throws ParseException {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new FlowLayout());
   
    MaskFormatter mf = new MaskFormatter("A-AAAA-AAAA-A");
    
    JFormattedTextField ftf1 = new JFormattedTextField(mf);
    
    mf.setValueContainsLiteralCharacters(false);
    
    System.out.println(mf.valueToString("123123123123"));
     
    
    f.add(ftf1);
    f.add(new JTextField(5));
    f.setSize(300, 100);
    f.setVisible(true);
  }
}

The code above generates the following result.

Write the formatting mask with Characters you can use in Java

Custom

The following table shows the characters we can use to custom JFormattedTextField.


Character          Description//from ww w  . j  a va 2  s  . c  o  m
#                  Any valid number (Character.isDigit).
'(single quote)    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.
A                  Any character or number (Character.isLetter or Character.isDigit).
?                  Any character (Character.isLetter).
*                  Anything.
H                  Any hex character (0-9, a-f or A-F).




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer