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) 

Source Link

Document

Creates a JFormattedTextField with the specified AbstractFormatterFactory.

Usage

From source file:DefaultFormatterFactoryDemo.java

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

    JLabel label = new JLabel("Date");

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DateFormat nullFormat = new SimpleDateFormat("'null'");
    DateFormatter nullFormatter = new DateFormatter(nullFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter, nullFormatter);

    JFormattedTextField input = new JFormattedTextField(factory);
    input.setColumns(30);// ww  w .  j a  v a2 s .c o  m
    JPanel panel = new JPanel();
    panel.add(label);
    panel.add(input);
    frame.add(panel, "North");

    frame.add(new JTextField(), "Center");
    frame.pack();
    frame.setVisible(true);
}

From source file:RegexPatternFormatter.java

public static void main(String argv[]) {
    // a demo main() to show how RegexPatternFormatter could be used

    JLabel lab1 = new JLabel("even length strings:");
    java.util.regex.Pattern evenLength = java.util.regex.Pattern.compile("(..)*");
    JFormattedTextField ftf1 = new JFormattedTextField(new RegexPatternFormatter(evenLength));

    JLabel lab2 = new JLabel("no vowels:");
    java.util.regex.Pattern noVowels = java.util.regex.Pattern.compile("[^aeiou]*",
            java.util.regex.Pattern.CASE_INSENSITIVE);
    RegexPatternFormatter noVowelFormatter = new RegexPatternFormatter(noVowels);
    noVowelFormatter.setAllowsInvalid(false); // don't allow user to type
    // vowels//from w ww  .j a  va 2s.co m
    JFormattedTextField ftf2 = new JFormattedTextField(noVowelFormatter);

    JFrame f = new JFrame("RegexPatternFormatter Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pan1 = new JPanel(new java.awt.BorderLayout());
    pan1.add(lab1, java.awt.BorderLayout.WEST);
    pan1.add(ftf1, java.awt.BorderLayout.CENTER);
    lab1.setLabelFor(ftf1);
    f.getContentPane().add(pan1, java.awt.BorderLayout.NORTH);
    JPanel pan2 = new JPanel(new java.awt.BorderLayout());
    pan2.add(lab2, java.awt.BorderLayout.WEST);
    pan2.add(ftf2, java.awt.BorderLayout.CENTER);
    lab2.setLabelFor(ftf2);
    f.getContentPane().add(pan2, java.awt.BorderLayout.SOUTH);
    f.setSize(300, 80);
    f.setVisible(true);
}

From source file:CombinationFormatter.java

public static void main(String argv[]) {
    // a demo main() to show how CombinationFormatter could be used
    int comb1[] = { 35, 11, 19 };
    int comb2[] = { 10, 20, 30 };

    final JFormattedTextField field1 = new JFormattedTextField(new CombinationFormatter());
    field1.setValue(comb1);/*  w w  w  . j  a v  a  2s .c  om*/

    final JFormattedTextField field2 = new JFormattedTextField(new CombinationFormatter());
    field2.setValue(comb2);

    JPanel pan = new JPanel();
    pan.add(new JLabel("Change the combination from"));
    pan.add(field1);
    pan.add(new JLabel("to"));
    pan.add(field2);

    JButton b = new JButton("Submit");
    b.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent ae) {
            try {
                field1.commitEdit(); // make sure current edit (if any) gets
                // committed
                field2.commitEdit();
            } catch (java.text.ParseException pe) {
            }
            int oldc[] = (int[]) field1.getValue();
            int newc[] = (int[]) field2.getValue();
            //
            // code to validate oldc[] and change to newc[] goes here
            //
        }
    });
    pan.add(b);

    JFrame f = new JFrame("CombinationFormatter Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(pan);
    f.setSize(360, 100);
    f.setVisible(true);
}

From source file:RegexFormatter.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFormattedTextField formattedField = new JFormattedTextField(
            new RegexFormatter("\\(\\d{3}\\)\\d{3}-\\d{4}"));

    frame.add(formattedField, "North");

    frame.add(new JTextField(), "South");

    frame.setSize(300, 200);/*  w  w  w  . j  a v  a2  s .  c o m*/
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Custom creation method for {@link JFormattedTextField}.
 *///from w ww .  j  ava 2  s.c o m
public static JFormattedTextField createIntegerTextField(final int min, final int max, final int now,
        final int columnNumber) {
    final NumberFormatter formatter = new NumberFormatter(NumberFormat.getIntegerInstance());
    formatter.setMinimum(min);
    formatter.setMaximum(max);
    final JFormattedTextField TF = new JFormattedTextField(formatter);
    TF.setValue(now);
    TF.setColumns(columnNumber);
    return TF;
}

From source file:MainClass.java

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

    MaskFormatter lowercase = null;
    try {/*from  ww w .ja v  a  2 s.co m*/
        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;
}

From source file:MainClass.java

public static JPanel demo1() {
    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 1: format toggles with focus"));

    MaskFormatter withFocus = null, withoutFocus = null;
    try {//from w  w  w. j a  v a2  s .co m
        withFocus = new MaskFormatter("LLLL");
        withoutFocus = new MaskFormatter("UUUU");
    } catch (ParseException pe) {
    }

    DefaultFormatterFactory factory = new DefaultFormatterFactory(withoutFocus, null, withFocus);

    JFormattedTextField field = new JFormattedTextField(factory);
    field.setValue("Four");
    pan.add(field, BorderLayout.CENTER);

    return pan;
}

From source file:Main.java

public static final JFormattedTextField createMaskFormattedTextField(String format) {
    try {/*www  .j a  va 2  s  . co  m*/
        MaskFormatter mf = new MaskFormatter(format);
        JFormattedTextField field = new JFormattedTextField(mf);
        return field;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public Main() {
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Number :");
    JFormattedTextField tf = new JFormattedTextField(NumberFormat.getIntegerInstance());

    tf.setColumns(10);//w w  w . j ava  2 s  .  co m
    panel.add(label);
    panel.add(tf);
    JButton button = new JButton("Click Me");
    panel.add(button);
    getContentPane().add(panel, BorderLayout.SOUTH);
    pack();
}

From source file:FactoryDemo.java

public static JPanel demo1() {
    // Demo 1: field with different formats with focus and without

    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 1: format toggles with focus"));

    MaskFormatter withFocus = null, withoutFocus = null;
    try {// w  w  w .j  a  v  a  2 s  . c o  m
        withFocus = new MaskFormatter("LLLL");
        withoutFocus = new MaskFormatter("UUUU");
    } catch (ParseException pe) {
    }

    DefaultFormatterFactory factory = new DefaultFormatterFactory(withoutFocus, null, withFocus);

    JFormattedTextField field = new JFormattedTextField(factory);
    field.setValue("Four");
    pan.add(field, BorderLayout.CENTER);

    return pan;
}