Example usage for javax.swing JFormattedTextField isEditValid

List of usage examples for javax.swing JFormattedTextField isEditValid

Introduction

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

Prototype

@BeanProperty(bound = false)
public boolean isEditValid() 

Source Link

Document

Returns true if the current value being edited is valid.

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 av a  2s  .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

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);

    JLayer jlayer = (JLayer) c;
    JFormattedTextField ftf = (JFormattedTextField) jlayer.getView();
    if (!ftf.isEditValid()) {
        Graphics2D g2 = (Graphics2D) g.create();

        // Paint the red X.
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int w = c.getWidth();
        int h = c.getHeight();
        int s = 8;
        int pad = 4;
        int x = w - pad - s;
        int y = (h - s) / 2;
        g2.setPaint(Color.red);/*  w ww . j a  v a  2  s  .  c o m*/
        g2.fillRect(x, y, s + 1, s + 1);
        g2.setPaint(Color.white);
        g2.drawLine(x, y, x + s, y + s);
        g2.drawLine(x, y + s, x + s, y);

        g2.dispose();
    }
}

From source file:components.IntegerEditor.java

public boolean stopCellEditing() {
    JFormattedTextField ftf = (JFormattedTextField) getComponent();
    if (ftf.isEditValid()) {
        try {//from w  w w . j  a v a2 s.  c o m
            ftf.commitEdit();
        } catch (java.text.ParseException exc) {
        }

    } else { //text is invalid
        if (!userSaysRevert()) { //user wants to edit
            return false; //don't let the editor go away
        }
    }
    return super.stopCellEditing();
}

From source file:FormatTest.java

public boolean verify(JComponent component) {
    JFormattedTextField field = (JFormattedTextField) component;
    return field.isEditValid();
}