Java JFormattedTextField get/set formatter

Description

Java JFormattedTextField get/set formatter

import java.awt.FlowLayout;
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:");
    //  w w w.j a  v  a2 s  .c o m
    JFormattedTextField name = new JFormattedTextField(new SimpleDateFormat(
        "yyyy-MM-dd"));
    name.setValue(new Date());
    
    DateFormatter fmt = (DateFormatter) name.getFormatter();
    fmt.setFormat(new SimpleDateFormat("d/M/yyyy"));
    
    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