Java JFormattedTextField create from DefaultFormatterFactory

Description

Java JFormattedTextField create from DefaultFormatterFactory

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 Main {
   public static void main(final String args[]) {
      JFrame frame = new JFrame("Formatted Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      DateFormat displayFormat = new SimpleDateFormat("yyyy/MM/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);//  w w  w .  j  av  a  2s.com
      frame.setVisible(true);
   }
}



PreviousNext

Related