Create a JFormattedTextField to Display and Edit a Phone Number(###-###-####) in Java
Description
The following code shows how to create a JFormattedTextField to Display and Edit a Phone Number(###-###-####).
Example
/*from w w w .j a v a 2s. c om*/
import java.awt.BorderLayout;
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(final String args[]) throws ParseException {
JFrame frame = new JFrame("Formatted Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MaskFormatter fmt = new MaskFormatter("###-###-####");
JFormattedTextField tft1 = new JFormattedTextField(fmt);
frame.add(tft1, BorderLayout.NORTH);
frame.add(new JTextField(), BorderLayout.SOUTH);
frame.setSize(250, 100);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »