Example usage for javax.swing JFormattedTextField PERSIST

List of usage examples for javax.swing JFormattedTextField PERSIST

Introduction

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

Prototype

int PERSIST

To view the source code for javax.swing JFormattedTextField PERSIST.

Click Source Link

Document

Constant identifying that when focus is lost, the edited value should be left.

Usage

From source file:Main.java

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

    JPanel contentPane = new JPanel();

    JFormattedTextField ftf = new JFormattedTextField(NumberFormat.getNumberInstance());
    ftf.setColumns(10);//from w w  w . j a  va  2s  . com
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
    ftf.setValue(100);
    lastValidValue = "100";
    ftf.addCaretListener(e -> {
        System.out.println("Last Valid Value : " + lastValidValue);
        if (ftf.isEditValid()) {
            String latestValue = ftf.getText();
            System.out.println("Latest Value : " + latestValue);
            if (!(latestValue.equals(lastValidValue)))
                ftf.setBackground(Color.YELLOW.darker());
            else {
                lastValidValue = ftf.getText();
                ftf.setBackground(Color.WHITE);
            }
        } else {
            System.out.println("Invalid Edit Entered.");
        }
    });
    contentPane.add(ftf);
    frame.setContentPane(contentPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:FieldValidator.java

private static JComponent createContent() {
    Dimension labelSize = new Dimension(80, 20);

    Box box = Box.createVerticalBox();

    // A single LayerUI for all the fields.
    LayerUI<JFormattedTextField> layerUI = new ValidationLayerUI();

    // Number field.
    JLabel numberLabel = new JLabel("Number:");
    numberLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    numberLabel.setPreferredSize(labelSize);

    NumberFormat numberFormat = NumberFormat.getInstance();
    JFormattedTextField numberField = new JFormattedTextField(numberFormat);
    numberField.setColumns(16);/*from  w w  w.  j  a v  a2  s  . c  om*/
    numberField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    numberField.setValue(42);

    JPanel numberPanel = new JPanel();
    numberPanel.add(numberLabel);
    numberPanel.add(new JLayer<JFormattedTextField>(numberField, layerUI));

    // Date field.
    JLabel dateLabel = new JLabel("Date:");
    dateLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    dateLabel.setPreferredSize(labelSize);

    DateFormat dateFormat = DateFormat.getDateInstance();
    JFormattedTextField dateField = new JFormattedTextField(dateFormat);
    dateField.setColumns(16);
    dateField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    dateField.setValue(new java.util.Date());

    JPanel datePanel = new JPanel();
    datePanel.add(dateLabel);
    datePanel.add(new JLayer<JFormattedTextField>(dateField, layerUI));

    // Time field.
    JLabel timeLabel = new JLabel("Time:");
    timeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    timeLabel.setPreferredSize(labelSize);

    DateFormat timeFormat = DateFormat.getTimeInstance();
    JFormattedTextField timeField = new JFormattedTextField(timeFormat);
    timeField.setColumns(16);
    timeField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    timeField.setValue(new java.util.Date());

    JPanel timePanel = new JPanel();
    timePanel.add(timeLabel);
    timePanel.add(new JLayer<JFormattedTextField>(timeField, layerUI));

    // Put them all in the box.
    box.add(Box.createGlue());
    box.add(numberPanel);
    box.add(Box.createGlue());
    box.add(datePanel);
    box.add(Box.createGlue());
    box.add(timePanel);

    return box;
}

From source file:components.IntegerEditor.java

public IntegerEditor(int min, int max) {
    super(new JFormattedTextField());
    ftf = (JFormattedTextField) getComponent();
    minimum = new Integer(min);
    maximum = new Integer(max);

    //Set up the editor for the integer cells.
    integerFormat = NumberFormat.getIntegerInstance();
    NumberFormatter intFormatter = new NumberFormatter(integerFormat);
    intFormatter.setFormat(integerFormat);
    intFormatter.setMinimum(minimum);//w  ww  . j ava2 s  .com
    intFormatter.setMaximum(maximum);

    ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
    ftf.setValue(minimum);
    ftf.setHorizontalAlignment(JTextField.TRAILING);
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

    //React when the user presses Enter while the editor is
    //active.  (Tab is handled as specified by
    //JFormattedTextField's focusLostBehavior property.)
    ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    ftf.getActionMap().put("check", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (!ftf.isEditValid()) { //The text is invalid.
                if (userSaysRevert()) { //reverted
                    ftf.postActionEvent(); //inform the editor
                }
            } else
                try { //The text is valid,
                    ftf.commitEdit(); //so use it.
                    ftf.postActionEvent(); //stop editing
                } catch (java.text.ParseException exc) {
                }
        }
    });
}

From source file:gda.gui.beans.BeanBase.java

protected JTextField getTxtValue() {
    if (txtValue == null) {
        txtValue = new JFormattedTextField();
        // When the text box loses focus, keep the edited value. This will be reverted eventually by the
        // main update loop
        //TODO this will cause the text to revert from 1700 to 1,700 . We need to set the formatter to simple or do not use this box at all.
        //Currently we handle the comma in the txtValueActionPerformed
        txtValue.setFocusLostBehavior(JFormattedTextField.PERSIST);

        txtValue.setValue(new Double(100));
        txtValue.setText("position");
        txtValue.setHorizontalAlignment(SwingConstants.RIGHT);
        txtValue.setPreferredSize(new java.awt.Dimension(60, 19));
        txtValue.setMinimumSize(new java.awt.Dimension(60, 19));
        txtValue.addActionListener(new ActionListener() {
            @Override/*from  w  ww  . ja va2s .  c om*/
            public void actionPerformed(ActionEvent evt) {

                // Remove focus from the text field, so it updates as the motor moves
                requestFocus();

                // do this in a new thread that's not the event thread.
                uk.ac.gda.util.ThreadManager.getThread(new Runnable() {
                    @Override
                    public void run() {
                        manualUpdate = true;
                        txtValueActionPerformed();
                    }
                }).start();
            }
        });
    }
    return txtValue;
}