Java Reference
In-Depth Information
JLabel label;
JFormattedTextField input;
JPanel panel;
MaskFormatter formatter;
BoxLayout layout =
new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
try {
label = new JLabel("SSN");
formatter = new MaskFormatter("###'-##'-####");
input = new JFormattedTextField(formatter);
input.setValue("123-45-6789");
input.setColumns(20);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
} catch (ParseException e) {
System.err.println("Unable to add SSN");
}
try {
label = new JLabel("US Phone");
formatter = new MaskFormatter("'(###')' ###'-####");
input = new JFormattedTextField(formatter);
input.setColumns(20);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
} catch (ParseException e) {
System.err.println("Unable to add Phone");
}
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 16-8 shows the output of the program. In this example, the SSN field starts with an
initial value, while the phone number field does not.
Search WWH ::




Custom Search