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[] argv) {
    JFormattedTextField tft1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    tft1.setValue(new Integer(123));

    Integer intValue = (Integer) tft1.getValue();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFormattedTextField tft2 = new JFormattedTextField(new DecimalFormat("#.0"));
    tft2.setValue(new Float(123.4F));

    // Retrieve the value from the text field
    Float floatValue = (Float) tft2.getValue();

}

From source file:Main.java

public static void main(String[] argv) {

    JFormattedTextField tft2 = new JFormattedTextField(DateFormat.getDateInstance(DateFormat.SHORT));
    tft2.setValue(new Date());

}

From source file:Main.java

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

    JFormattedTextField tft3 = new JFormattedTextField(new SimpleDateFormat("yyyy-M-d"));
    tft3.setValue(new Date());

    Date date = (Date) tft3.getValue();
}

From source file:Main.java

public static void main(String[] argv) {
    JFormattedTextField f = new JFormattedTextField(new SimpleDateFormat("yyyy-M-d"));
    f.setValue(new Date());

    DateFormatter fmt = (DateFormatter) f.getFormatter();
    fmt.setFormat(new SimpleDateFormat("d/M/yyyy"));

    f.setValue(f.getValue());/*from  ww  w  .jav a 2 s . c  om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    MaskFormatter fmt = new MaskFormatter("###-###-####");

    JFormattedTextField tft1 = new JFormattedTextField(fmt);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    MaskFormatter fmt = new MaskFormatter("###-###-####");

    JFormattedTextField tft1 = new JFormattedTextField(fmt);

    fmt = new MaskFormatter("###-##-####");

    JFormattedTextField tft2 = new JFormattedTextField(fmt);
}

From source file:Main.java

public static void main(String[] argv) {
    JFormattedTextField f = new JFormattedTextField(new BigDecimal("123.4567"));
    DefaultFormatter fmt = new NumberFormatter(new DecimalFormat("#.0###############"));
    fmt.setValueClass(f.getValue().getClass());
    DefaultFormatterFactory fmtFactory = new DefaultFormatterFactory(fmt, fmt, fmt);
    f.setFormatterFactory(fmtFactory);/*from w w  w.  j a  v a 2  s  .  co  m*/

    BigDecimal bigValue = (BigDecimal) f.getValue();

}

From source file:FormattedTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Formatted");
    Container contentPane = frame.getContentPane();
    JFormattedTextField ftf1 = new JFormattedTextField(new Integer(0));
    contentPane.add(ftf1, BorderLayout.NORTH);
    JFormattedTextField ftf2 = new JFormattedTextField(new Date());
    contentPane.add(ftf2, BorderLayout.SOUTH);
    frame.setSize(200, 100);//from   w w w  . j a  v  a  2 s  .c  om
    frame.show();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    MaskFormatter formatter = new MaskFormatter("###-##-####");
    JFormattedTextField tf = new JFormattedTextField(formatter);
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(tf, BorderLayout.NORTH);
    JButton clickBtn = new JButton("Click me!");
    clickBtn.addActionListener(e -> {
        if (!tf.getText().matches(formatter.getMask())) {
            System.err.println("Your Input does not match the pattern!");
        } else {/*from w  w w  .jav  a 2  s .com*/
            System.out.println(tf.getText());
        }
    });
    panel.add(clickBtn, BorderLayout.SOUTH);
    JFrame f = new JFrame();

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(panel, BorderLayout.CENTER);
    f.setSize(800, 600);
    f.setVisible(true);
}