Example usage for javax.swing JFormattedTextField setFocusLostBehavior

List of usage examples for javax.swing JFormattedTextField setFocusLostBehavior

Introduction

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

Prototype

@BeanProperty(bound = false, enumerationValues = { "JFormattedTextField.COMMIT",
        "JFormattedTextField.COMMIT_OR_REVERT", "JFormattedTextField.REVERT",
        "JFormattedTextField.PERSIST" }, description = "Behavior when component loses focus")
public void setFocusLostBehavior(int behavior) 

Source Link

Document

Sets the behavior when focus is lost.

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);/* www  .  j  a v a  2  s  . c om*/
    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);/* ww w.  j a va  2s.c o m*/
    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:FormatTest.java

public FormatTestFrame() {
    setTitle("FormatTest");
    setSize(WIDTH, HEIGHT);/*from ww w.j a v  a  2s  . c om*/

    JPanel buttonPanel = new JPanel();
    okButton = new JButton("Ok");
    buttonPanel.add(okButton);
    add(buttonPanel, BorderLayout.SOUTH);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 3));
    add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField.setValue(new Integer(100));
    addRow("Number:", intField);

    JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField2.setValue(new Integer(100));
    intField2.setFocusLostBehavior(JFormattedTextField.COMMIT);
    addRow("Number (Commit behavior):", intField2);

    JFormattedTextField intField3 = new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
                protected DocumentFilter getDocumentFilter() {
                    return filter;
                }

                private DocumentFilter filter = new IntFilter();
            });
    intField3.setValue(new Integer(100));
    addRow("Filtered Number", intField3);

    JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField4.setValue(new Integer(100));
    intField4.setInputVerifier(new FormattedTextFieldVerifier());
    addRow("Verified Number:", intField4);

    JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
    currencyField.setValue(new Double(10));
    addRow("Currency:", currencyField);

    JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance());
    dateField.setValue(new Date());
    addRow("Date (default):", dateField);

    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
    format.setLenient(false);
    JFormattedTextField dateField2 = new JFormattedTextField(format);
    dateField2.setValue(new Date());
    addRow("Date (short, not lenient):", dateField2);

    try {
        DefaultFormatter formatter = new DefaultFormatter();
        formatter.setOverwriteMode(false);
        JFormattedTextField urlField = new JFormattedTextField(formatter);
        urlField.setValue(new URL("http://java.sun.com"));
        addRow("URL:", urlField);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        MaskFormatter formatter = new MaskFormatter("###-##-####");
        formatter.setPlaceholderCharacter('0');
        JFormattedTextField ssnField = new JFormattedTextField(formatter);
        ssnField.setValue("078-05-1120");
        addRow("SSN Mask:", ssnField);
    } catch (ParseException exception) {
        exception.printStackTrace();
    }

    JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter());
    ipField.setValue(new byte[] { (byte) 130, 65, 86, 66 });
    addRow("IP Address:", ipField);
}

From source file:org.revager.tools.GUITools.java

/**
 * Formats the given spinner./*from  w  ww  .ja  v a2s.  com*/
 * 
 * @param sp
 *            the spinner
 */
public static void formatSpinner(JSpinner sp, boolean hideBorder) {
    JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor) sp.getEditor();
    JFormattedTextField ftf = defEditor.getTextField();
    ftf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
    InternationalFormatter intFormatter = (InternationalFormatter) ftf.getFormatter();
    DecimalFormat decimalFormat = (DecimalFormat) intFormatter.getFormat();
    decimalFormat.applyPattern("00");
    DecimalFormatSymbols geSymbols = new DecimalFormatSymbols(Data.getInstance().getLocale());
    decimalFormat.setDecimalFormatSymbols(geSymbols);

    if (hideBorder) {
        sp.setBorder(null);
    }
}