import java.awt.BorderLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;
public class FormattedSample {
public static void main(final String args[]) {
JFrame frame = new JFrame("Formatted Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter displayFormatter = new DateFormatter(displayFormat);
DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
DateFormatter editFormatter = new DateFormatter(editFormat);
DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
displayFormatter, editFormatter);
JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
frame.add(date2TextField, BorderLayout.NORTH);
frame.add(new JTextField(), BorderLayout.SOUTH);
frame.setSize(250, 100);
frame.setVisible(true);
}
}
|