Java Swing How to - Install InputVerifier for JFormattedTextField








Question

We would like to know how to install InputVerifier for JFormattedTextField.

Answer

import java.text.ParseException;
import java.text.SimpleDateFormat;
// w  ww. j a  v a 2s. co  m
import javax.swing.Box;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

public class Main {
  SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");

  public Main() {
    JFormattedTextField formattedField = null;
    try {
      MaskFormatter dateMask = new MaskFormatter("##/##/####");
      formattedField = new JFormattedTextField(dateMask);
    } catch (ParseException ex) {
      System.out.println(ex);
    }
    formattedField.setColumns(10);
    formattedField.setInputVerifier(getInputVerifier());
    
    JTextField field = new JTextField(10);
    format.setLenient(false);

    Box box = Box.createVerticalBox();
    box.add(formattedField);
    box.add(Box.createVerticalStrut(10));
    box.add(field);
    box.setBorder(new EmptyBorder(10, 10, 10, 10));

    JFrame frame = new JFrame();
    frame.add(box);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
  private InputVerifier getInputVerifier() {
    InputVerifier verifier = new InputVerifier() {
      @Override
      public boolean verify(JComponent input) {
        JFormattedTextField field = (JFormattedTextField) input;
        String text = field.getText();
        return isValidDate(text);
      }

      @Override
      public boolean shouldYieldFocus(JComponent input) {
        boolean valid = verify(input);
        if (!valid) {
          JOptionPane.showMessageDialog(null,
              "Please enter a valid date in format dd/mm/yyyy");
        }
        return valid;
      }
    };
    return verifier;
  }

  public boolean isValidDate(String dateString) {
    try {
      format.parse(dateString);
      return true;
    } catch (ParseException ex) {
      return false;
    }
  }
  public static void main(String[] args) {
    new Main();
  }
}