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 Main() {
    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String dateString = formatter.format(date);
    formatText = new JFormattedTextField(createFormatter("####-##-## ##:##:##"));
    formatText.setColumns(20);/*from ww  w  . j  a va2  s  .  co m*/
    formatText.setText(dateString);

    setLayout(new BorderLayout());
    add(new JLabel("Enter Date and Time in YYYY-MM-DD HH:MM:SS format"), BorderLayout.NORTH);
    add(formatText, BorderLayout.CENTER);
}

From source file:SimpleFTF.java

public SimpleFTF() {
    JFormattedTextField ftf[] = new JFormattedTextField[7];
    String des[] = new String[ftf.length]; // description of each field

    des[0] = "Date";
    ftf[0] = new JFormattedTextField(new java.util.Date());

    des[1] = "Integer";
    ftf[1] = new JFormattedTextField(new Integer(90032221));

    des[2] = "Float";
    ftf[2] = new JFormattedTextField(new Float(3.14));

    des[3] = "Float work-around"; // manually specify a NumberFormat
    ftf[3] = new JFormattedTextField(java.text.NumberFormat.getInstance());
    ftf[3].setValue(new Float(3.14));

    des[4] = "currency";
    ftf[4] = new JFormattedTextField(java.text.NumberFormat.getCurrencyInstance());
    ftf[4].setValue(new Float(5.99));

    des[5] = "percent";
    ftf[5] = new JFormattedTextField(java.text.NumberFormat.getPercentInstance());
    ftf[5].setValue(new Float(0.33));

    des[6] = "java.net.URL"; // works via 1-arg String constructor and
    // toString()
    java.net.URL u = null;//from w w  w  .  ja va 2  s .  com
    try {
        u = new java.net.URL("http://www.ora.com/");
    } catch (java.net.MalformedURLException ignored) {
    }
    ftf[6] = new JFormattedTextField(u);
    ftf[6].setColumns(24);

    // add each ftf[] to a BoxLayout
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    for (int j = 0; j < ftf.length; j += 1) {
        JPanel borderPanel = new JPanel(new java.awt.BorderLayout());
        borderPanel.setBorder(new javax.swing.border.TitledBorder(des[j]));
        borderPanel.add(ftf[j], java.awt.BorderLayout.CENTER);
        add(borderPanel);
    }
}

From source file:Main.java

public Main() {
    JFormattedTextField formattedField = null;
    try {//w w w  .j  a va  2 s .c  om
        MaskFormatter dateMask = new MaskFormatter("##/##/####");
        formattedField = new JFormattedTextField(dateMask);
    } catch (ParseException ex) {
        System.out.println(ex);
    }
    formattedField.setColumns(10);
    formattedField.setInputVerifier(getInputVerifier());

    JTextField field = new JTextField(10);
    format.setLenient(false);

    Box box = Box.createVerticalBox();
    box.add(formattedField);
    box.add(Box.createVerticalStrut(10));
    box.add(field);
    box.setBorder(new EmptyBorder(10, 10, 10, 10));

    JFrame frame = new JFrame();
    frame.add(box);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    amountDisplayFormat = NumberFormat.getCurrencyInstance();
    System.out.println(amountDisplayFormat.format(1200));
    amountDisplayFormat.setMinimumFractionDigits(0);
    amountEditFormat = NumberFormat.getNumberInstance();
    amountField = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(amountDisplayFormat),
            new NumberFormatter(amountDisplayFormat), new NumberFormatter(amountEditFormat)));
    amountField.setValue(new Double(amount));
    amountField.setColumns(10);/*from  ww w .  j ava2s .c  om*/
    amountField.addPropertyChangeListener("value", this);

    JPanel fieldPane = new JPanel(new GridLayout(0, 1));
    fieldPane.add(amountField);

    add(fieldPane, BorderLayout.LINE_END);
    add(new JButton("Hello"), BorderLayout.SOUTH);
}

From source file:MyFormatter.java

public Main() {
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Number :");
    JFormattedTextField tf = new JFormattedTextField(new MyFormatter());
    tf.setColumns(10);//from ww w.  ja  v  a 2 s  .  c o m
    panel.add(label);
    panel.add(tf);
    getContentPane().add(panel, BorderLayout.SOUTH);
    pack();
}

From source file:Main.java

private void initComponents() {
    JFrame frame = new JFrame("JFormattedTextField Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    MaskFormatter mask = null;/*from  www .  java 2 s.  co  m*/
    try {
        mask = new MaskFormatter("##h##min##s");// the # is for numeric values
        mask.setPlaceholderCharacter('#');
    } catch (ParseException e) {
        e.printStackTrace();
    }
    final JFormattedTextField timeField = new JFormattedTextField(mask);

    // ActionListener for when enter is pressed
    timeField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            Object source = ae.getSource();
            if (source == timeField) {
                // parse to a valid time here
                System.out.println(timeField.getText());
            }
        }
    });
    frame.add(timeField);
    frame.pack();
    frame.setVisible(true);
}

From source file:FactoryDemo.java

public static JPanel demo2() {
    // Demo 2: Change the format of a field when the user presses a button.
    // We can't call field.setFormatter() because that's a protected method.
    // (Plus it wouldn't work anyway. The old factory would replace our new
    // formatter with an "old" one next time the field gains or loses
    // focus.)// w w  w . jav a 2s  .com
    // The thing to do is send a new factory to field.setFormatterFactory().

    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 2: change format midstream"));

    MaskFormatter lowercase = null;
    try {
        lowercase = new MaskFormatter("LLLL");
    } catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("Fore");
    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(); // commit current edit (if any)
                MaskFormatter uppercase = new MaskFormatter("UUUU");
                DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
                field.setFormatterFactory(factory);
                change.setEnabled(false);
            } catch (ParseException pe) {
            }
        }
    });

    return pan;
}

From source file:Main.java

public Main() {
    formTextFieldFormat = NumberFormat.getNumberInstance();
    formTextFieldFormat.setMinimumFractionDigits(2);
    formTextFieldFormat.setMaximumFractionDigits(2);
    formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

    focusLabel.setPreferredSize(new Dimension(120, 27));
    formTextField = new JFormattedTextField(formTextFieldFormat);
    formTextField.setValue(amount);//  w  ww .j  a v a 2  s. co m
    formTextField.setPreferredSize(new Dimension(120, 27));
    formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField.requestFocus();
            formTextField.setText(formTextField.getText());
            formTextField.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
            double t1a1 = (((Number) formTextField.getValue()).doubleValue());
            if (t1a1 < 1000) {
                formTextField.setValue(amount);
            }
        }
    });

    docLabel.setPreferredSize(new Dimension(120, 27));

    formTextField1 = new JFormattedTextField(formTextFieldFormat);
    formTextField1.setValue(amount);

    formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField1.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField1.requestFocus();
            formTextField1.setText(formTextField1.getText());
            formTextField1.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
        }
    });
    formTextField1.getDocument().addDocumentListener(docListener);

    pnl = new JPanel();
    pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
    pnl.setLayout(new GridLayout(2, 2));
    pnl.add(focusLabel);
    pnl.add(formTextField);
    pnl.add(docLabel);
    pnl.add(formTextField1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pnl, BorderLayout.CENTER);
    frame.setLocation(200, 200);
    frame.pack();
    frame.setVisible(true);
    formTextFieldFocus1();
}

From source file:Main.java

private JFormattedTextField setFormat(JFormattedTextField jft, Locale local1, int minFra, int maxFra) {
    NumberFormat numberFormat;/*  w  ww . j  a  v  a2 s  .c om*/
    Locale local = local1;
    int setMin = minFra;
    int setMax = maxFra;
    numberFormat = NumberFormat.getCurrencyInstance(local);
    numberFormat.setMinimumFractionDigits(setMin);
    numberFormat.setMaximumFractionDigits(setMax);
    numberFormat.setRoundingMode(RoundingMode.HALF_UP);
    jft = new JFormattedTextField(numberFormat);
    jft.setValue(new Double(342.796));
    return jft;
}

From source file:Main.java

protected JFormattedTextField createField() {
    MaskFormatter formatter = null;
    try {/*from   w  w  w  . j  a  v  a 2s . com*/
        formatter = new MaskFormatter("########");
    } catch (ParseException e) {
        e.printStackTrace(System.out);
    }
    JFormattedTextField jtf = new JFormattedTextField(formatter);
    return jtf;
}