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. |
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). |
import java.awt.FlowLayout; import java.text.ParseException; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.MaskFormatter; public class Main extends JFrame { public Main() { super("JButton"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); JLabel nameLabel = new JLabel("Value:"); //w ww.j av a 2 s . c o m MaskFormatter fmt = null; try { fmt = new MaskFormatter("###-##-####"); } catch (ParseException e) { e.printStackTrace(); } fmt.setPlaceholderCharacter('*'); JFormattedTextField name = new JFormattedTextField(fmt); JTextField text = new JTextField("Click here to see the validation result"); getContentPane().add(nameLabel); getContentPane().add(name); getContentPane().add(text); } public static void main(String[] args) { Main frame = new Main(); frame.pack(); frame.setVisible(true); } }