Java Swing DateFormatter class

Description

Java Swing DateFormatter class

import java.awt.FlowLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.DateFormatter;

public class Main extends JFrame {
  public Main() {
    super("JButton");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    JLabel nameLabel = new JLabel("Value:");
    //from w  ww  . j  a v a  2  s  .  c om
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");
    DateFormatter dateFormatter = new DateFormatter(dateFormat);

    JFormattedTextField name = new JFormattedTextField(dateFormatter);
    name.setValue(new Date());
    JTextField text = new JTextField("Click here to see the validation result");

    getContentPane().add(nameLabel);
    getContentPane().add(name);
    getContentPane().add(text);

  }


  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related