JFormattedTextField: setFormatterFactory(AbstractFormatterFactory tf) : JFormattedTextField « javax.swing « Java by API






JFormattedTextField: setFormatterFactory(AbstractFormatterFactory tf)

  
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

public class MainClass {

  public static JPanel demo1() {
    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("change format midstream"));

    MaskFormatter lowercase = null;
    try {
      lowercase = new MaskFormatter("LLLL");
    } catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("lower case");
    pan.add(field, BorderLayout.CENTER);

    final JButton change = new JButton("change format");
    JPanel changePanel = new JPanel();
    changePanel.add(change);
    pan.add(changePanel, BorderLayout.SOUTH);

    change.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        try {
          field.commitEdit();
          MaskFormatter uppercase = new MaskFormatter("UUUU");
          DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
          field.setFormatterFactory(factory);
          change.setEnabled(false);
        } catch (ParseException pe) {
        }
      }
    });

    return pan;
  }

  public static void main(String argv[]) {
    JFrame f = new JFrame("FactoryDemo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(demo1(), BorderLayout.NORTH);
    f.getContentPane().add(new JTextField(), BorderLayout.SOUTH);
    f.setSize(240, 160);
    f.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: addActionListener(ActionListener l)
9.JFormattedTextField: commitEdit()
10.JFormattedTextField: getFormatterFactory()
11.JFormattedTextField: getValue()
12.JFormattedTextField: isEditValid()
13.JFormattedTextField: setColumns(int columns)
14.JFormattedTextField: setFont(Font f)
15.JFormattedTextField: setInputVerifier(InputVerifier inputVerifier)
16.JFormattedTextField: setValue(Object value)
17.JFormattedTextField deals with URL