new JFormattedTextField(new MaskFormatter('(###)###-####'))
import java.util.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import javax.swing.Box;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
public class MainClass {
public static void main(String[] args) throws Exception {
Box form = Box.createVerticalBox();
form.add(new JLabel("Name:"));
form.add(new JTextField("User Name"));
form.add(new JLabel("Birthday:"));
JFormattedTextField birthdayField = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"));
birthdayField.setValue(new Date());
form.add(birthdayField);
form.add(new JLabel("Age:"));
form.add(new JFormattedTextField(new Integer(32)));
form.add(new JLabel("Hairs on Body:"));
JFormattedTextField hairsField = new JFormattedTextField(new DecimalFormat("###,###"));
hairsField.setValue(new Integer(100000));
form.add(hairsField);
form.add(new JLabel("Phone Number:"));
JFormattedTextField phoneField = new JFormattedTextField(new MaskFormatter("(###)###-####"));
phoneField.setValue("(314)888-1234");
form.add(phoneField);
JFrame frame = new JFrame("User Information");
frame.getContentPane().add(form);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Related examples in the same category