Java JFormattedTextField change format

Description

Java JFormattedTextField change format

import java.awt.BorderLayout;
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;

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

      JFormattedTextField input = new JFormattedTextField(new SimpleDateFormat("yyyy-M-d"));
      input.setValue(new Date());

      DateFormatter fmt = (DateFormatter) input.getFormatter();
      fmt.setFormat(new SimpleDateFormat("d/M/yyyy"));


      frame.add(input, BorderLayout.NORTH);

      frame.add(new JTextField(), BorderLayout.SOUTH);
      frame.setSize(250, 100);//from   ww w .  ja  va2 s . c  o m
      frame.setVisible(true);
   }
}



PreviousNext

Related