Example usage for javax.swing JFormattedTextField getValue

List of usage examples for javax.swing JFormattedTextField getValue

Introduction

In this page you can find the example usage for javax.swing JFormattedTextField getValue.

Prototype

public Object getValue() 

Source Link

Document

Returns the last valid value.

Usage

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

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

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter);// w w w . j  av  a2s  . c  o m
    JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
    frame.add(date2TextField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
            Object value = source.getValue();
            System.out.println("Class: " + value.getClass());
            System.out.println("Value: " + value);
        }
    };
    date2TextField.addActionListener(actionListener);

    frame.add(new JTextField(), BorderLayout.SOUTH);
    frame.setSize(250, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

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

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter);//from   w  w w.j a va  2 s.  c om
    JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
    frame.add(date2TextField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
            Object value = source.getValue();
            System.out.println("Class: " + value.getClass());
            System.out.println("Value: " + value);
        }
    };
    date2TextField.addActionListener(actionListener);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    frame.setSize(250, 100);
    frame.setVisible(true);
}

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) {
    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);/* w  w w.  j  a v a  2  s . co  m*/

    BigDecimal bigValue = (BigDecimal) f.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 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 source file:Main.java

public static void main(String[] args) throws Exception {
    final MaskFormatter formatter = new TimeFormatter();
    formatter.setValueClass(java.util.Date.class);
    final JFormattedTextField tf2 = new JFormattedTextField(formatter);
    tf2.setValue(new Date());
    final JLabel label = new JLabel();
    JButton bt = new JButton("Show Value");
    bt.addActionListener(e -> {/*from   www. j  a va  2s  . c  o m*/
        System.out.println(" value 2 = " + tf2.getValue());
        System.out.println(" value 2 = " + tf2.getText());
        System.out.println("value class: " + formatter.getValueClass());
        label.setText(tf2.getText());
    });
    JFrame f = new JFrame();
    f.getContentPane().setLayout(new GridLayout());
    f.getContentPane().add(tf2);
    f.getContentPane().add(label);
    f.getContentPane().add(bt);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat format = new DecimalFormat("####.##");
    format.setMinimumFractionDigits(2);/*from   w  w  w  .ja  v  a 2  s .  c om*/
    final JFormattedTextField field1 = new JFormattedTextField(format);
    final JFormattedTextField field2 = new JFormattedTextField(format);
    field1.setColumns(15);
    field2.setColumns(15);
    JButton btn = new JButton(new AbstractAction("Multiply by 2") {

        @Override
        public void actionPerformed(ActionEvent e) {
            Number value = (Number) field1.getValue();
            if (value != null) {
                field2.setValue(2 * value.doubleValue());
            }
        }
    });

    JPanel panel = new JPanel();
    panel.add(field1);
    panel.add(btn);
    panel.add(field2);
    JOptionPane.showMessageDialog(null, panel);
}

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  ww.  ja  va2  s . 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);
}