Example usage for javax.swing JFormattedTextField JFormattedTextField

List of usage examples for javax.swing JFormattedTextField JFormattedTextField

Introduction

In this page you can find the example usage for javax.swing JFormattedTextField JFormattedTextField.

Prototype

public JFormattedTextField(AbstractFormatterFactory factory, Object currentValue) 

Source Link

Document

Creates a JFormattedTextField with the specified AbstractFormatterFactory and initial value.

Usage

From source file:FormattedSample.java

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

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter);// w  w w. j a v  a2 s.  c o m
    JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
    frame.add(date2TextField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
            Object value = source.getValue();
            System.out.println("Class: " + value.getClass());
            System.out.println("Value: " + value);
        }
    };
    date2TextField.addActionListener(actionListener);

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

From source file:MainClass.java

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

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter);/* w  w w.  j av a  2  s.  c om*/
    JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
    frame.add(date2TextField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
            Object value = source.getValue();
            System.out.println("Class: " + value.getClass());
            System.out.println("Value: " + value);
        }
    };
    date2TextField.addActionListener(actionListener);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    frame.setSize(250, 100);
    frame.setVisible(true);
}