JFormattedTextField: addActionListener(ActionListener l) : JFormattedTextField « javax.swing « Java by API






JFormattedTextField: addActionListener(ActionListener l)

  

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
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;
import javax.swing.text.DefaultFormatterFactory;

public class MainClass {
  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);
    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);
  }
}



           
         
    
  








Related examples in the same category

1.new JFormattedTextField(Format format)
2.new JFormattedTextField(Formatter formatter)
3.new JFormattedTextField(AbstractFormatterFactory factory, Object currentValue)
4.new JFormattedTextField(Object value)
5.new JFormattedTextField(new MaskFormatter('(###)###-####'))
6.new JFormattedTextField(new DecimalFormat('###,###'))
7.new JFormattedTextField(new SimpleDateFormat('MM/dd/yy'))
8.JFormattedTextField: commitEdit()
9.JFormattedTextField: getFormatterFactory()
10.JFormattedTextField: getValue()
11.JFormattedTextField: isEditValid()
12.JFormattedTextField: setColumns(int columns)
13.JFormattedTextField: setFont(Font f)
14.JFormattedTextField: setFormatterFactory(AbstractFormatterFactory tf)
15.JFormattedTextField: setInputVerifier(InputVerifier inputVerifier)
16.JFormattedTextField: setValue(Object value)
17.JFormattedTextField deals with URL