Java JFormattedTextField create from DateFormat

Description

Java JFormattedTextField create from DateFormat

import java.awt.BorderLayout;
import java.text.DateFormat;
import java.text.Format;
import java.util.Date;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {
   public static void main(final String args[]) {
      JFrame frame = new JFrame("Formatted Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Format shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
      JFormattedTextField input = new JFormattedTextField(shortDate);
      input.setValue(new Date());
      input.setColumns(20);/*  ww w . ja  va  2  s  . co m*/

      frame.add(input, BorderLayout.NORTH);

      frame.add(new JTextField(), BorderLayout.SOUTH);
      frame.setSize(250, 100);
      frame.setVisible(true);
   }
}



PreviousNext

Related