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:Main.java

public static void main(String args[]) throws ParseException {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

    MaskFormatter mf1 = new MaskFormatter("###-###-###");
    mf1.setPlaceholderCharacter('_');
    JFormattedTextField ftf1 = new JFormattedTextField(mf1);
    content.add(ftf1);/*ww  w.  j  av a2 s.  com*/

    MaskFormatter mf2 = new MaskFormatter("(###) ###-####");
    JFormattedTextField ftf2 = new JFormattedTextField(mf2);
    content.add(ftf2);
    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    JFrame frame = new JFrame("Number Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Font font = new Font("SansSerif", Font.BOLD, 16);

    JLabel label;//from  w ww . j  a  va 2s.  co m
    JFormattedTextField input;
    JPanel panel;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    label = new JLabel("Raw Number:");
    input = new JFormattedTextField(2424.50);
    input.setValue(2424.50);
    input.setColumns(20);
    input.setFont(font);
    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(label);
    panel.add(input);
    frame.add(panel);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Number Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Font font = new Font("SansSerif", Font.BOLD, 16);

    JLabel label;/*  w ww.j  a v  a 2 s  . c  o m*/
    JFormattedTextField input;
    JPanel panel;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    label = new JLabel("Raw Number:");
    input = new JFormattedTextField(2424.50);
    input.setValue(2424.50);
    input.setColumns(20);
    input.setFont(font);

    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(label);
    panel.add(input);
    frame.add(panel);

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

From source file:MainClass.java

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

    JLabel label;//from w  w  w . ja  v  a 2  s. c  o m
    JFormattedTextField input;
    JPanel panel;
    MaskFormatter formatter;

    try {
        label = new JLabel("US Phone");
        formatter = new MaskFormatter("'(###')' ###'-####");
        formatter.setPlaceholderCharacter('*');
        input = new JFormattedTextField(formatter);
        input.setColumns(20);
        panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.add(label);
        panel.add(input);
        frame.add(panel);
    } catch (ParseException e) {
        System.err.println("Unable to add Phone");
    }

    frame.pack();
    frame.setVisible(true);
}

From source file:MyFormatter.java

public static void main(String[] args) {
    JFormattedTextField tf = new JFormattedTextField(new MyFormatter());
    tf.setColumns(10);/*from  www .j  a v a  2 s .c  o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 75);/*from w w w.j  a va 2  s. co m*/
    JPanel content = new JPanel(new FlowLayout());
    frame.setContentPane(content);

    MaskFormatter formatter = new MaskFormatter("#");
    formatter.setValidCharacters("123456789");

    JFormattedTextField f1 = new JFormattedTextField(formatter);
    f1.setValue(null);
    f1.setColumns(1);

    content.add(f1);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
    textField1.setFormatterFactory(new AbstractFormatterFactory() {
        @Override/*from www  .j a v  a  2s .  c o  m*/
        public AbstractFormatter getFormatter(JFormattedTextField tf) {
            NumberFormat format = DecimalFormat.getInstance();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);
            format.setRoundingMode(RoundingMode.HALF_UP);
            InternationalFormatter formatter = new InternationalFormatter(format);
            formatter.setAllowsInvalid(false);
            formatter.setMinimum(0.0);
            formatter.setMaximum(1000.00);
            return formatter;
        }
    });
    Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
    attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
    final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01));
    textField2.setFormatterFactory(new AbstractFormatterFactory() {

        @Override
        public AbstractFormatter getFormatter(JFormattedTextField tf) {
            NumberFormat format = DecimalFormat.getInstance();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);
            format.setRoundingMode(RoundingMode.HALF_UP);
            InternationalFormatter formatter = new InternationalFormatter(format);
            formatter.setAllowsInvalid(false);
            formatter.setMinimum(0.0);
            formatter.setMaximum(1000.00);
            return formatter;
        }
    });
    textField2.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            double t1a1 = (((Number) textField2.getValue()).doubleValue());
            if (t1a1 > 100) {
                textField2.setFont(new Font(attributes));
                textField2.setForeground(Color.red);
            } else {
                textField2.setFont(new Font("Serif", Font.BOLD, 16));
                textField2.setForeground(Color.black);
            }
        }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.SOUTH);
    frame.setVisible(true);
    frame.pack();
}

From source file:MaskInputSample.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Mask Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label;//from   w w w  .  j a v a 2 s.  c om
    JFormattedTextField input;
    JPanel panel;
    MaskFormatter formatter;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    try {
        label = new JLabel("SSN");
        formatter = new MaskFormatter("###'-##'-####");
        input = new JFormattedTextField(formatter);
        input.setValue("123-45-6789");
        input.setColumns(20);
        panel = new JPanel();
        panel.add(label);
        panel.add(input);
        frame.add(panel);
    } catch (ParseException e) {
        System.err.println("Unable to add SSN");
    }
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);//from w w w  .  ja  v  a2  s  .c o m
    format.setMinimumFractionDigits(2);
    format.setParseIntegerOnly(true);
    format.setRoundingMode(RoundingMode.HALF_UP);

    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMaximum(1000);
    formatter.setMinimum(0.0);
    formatter.setAllowsInvalid(false);
    // formatter.setOverwriteMode(false);

    JFormattedTextField tf = new JFormattedTextField(formatter);
    tf.setColumns(10);
    tf.setValue(123456789.99);
    JFormattedTextField tf1 = new JFormattedTextField(formatter);
    tf1.setValue(1234567890.99);
    JFormattedTextField tf2 = new JFormattedTextField(formatter);
    tf2.setValue(1111.1111);
    JFormattedTextField tf3 = new JFormattedTextField(formatter);
    tf3.setValue(-1111.1111);
    JFormattedTextField tf4 = new JFormattedTextField(formatter);
    tf4.setValue(-56);

    JFrame frame = new JFrame("Test");
    frame.setLayout(new GridLayout(5, 0));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tf);
    frame.add(tf1);
    frame.add(tf2);
    frame.add(tf3);
    frame.add(tf4);
    frame.pack();
    frame.setVisible(true);
}

From source file:JFormattedTextFieldDateInputSampleDateFormatSHORT.java

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

    JLabel label;/*from   w w  w .  java  2  s  . c o  m*/
    JFormattedTextField input;
    JPanel panel;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    Format shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
    label = new JLabel("Short date:");
    input = new JFormattedTextField(shortDate);
    input.setValue(new Date());
    input.setColumns(20);

    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(label);
    panel.add(input);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}