Example usage for javax.swing InputVerifier InputVerifier

List of usage examples for javax.swing InputVerifier InputVerifier

Introduction

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

Prototype

InputVerifier

Source Link

Usage

From source file:Main.java

public static void main(String args[]) {

    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent comp) {
            boolean returnValue;
            JTextField textField = (JTextField) comp;
            try {
                Integer.parseInt(textField.getText());
                returnValue = true;/*from   w  w  w  .ja  v  a 2 s .  c  o m*/
            } catch (NumberFormatException e) {
                returnValue = false;
            }
            return returnValue;
        }
    };
    textField1.setInputVerifier(verifier);

    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.CENTER);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent comp) {
            boolean returnValue;
            JTextField textField = (JTextField) comp;
            try {
                Integer.parseInt(textField.getText());
                returnValue = true;/*from   ww w .  jav  a  2s .  com*/
            } catch (NumberFormatException e) {
                returnValue = false;
            }
            return returnValue;
        }
    };
    textField1.setInputVerifier(verifier);
    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.CENTER);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    JTextField textField3 = new JTextField();

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent comp) {
            boolean returnValue;
            JTextField textField = (JTextField) comp;
            try {
                Integer.parseInt(textField.getText());
                returnValue = true;//from w w w  .  j  a va 2s  .c  om
            } catch (NumberFormatException e) {
                returnValue = false;
            }
            return returnValue;
        }
    };

    textField1.setInputVerifier(verifier);
    textField3.setInputVerifier(verifier);

    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.CENTER);
    frame.add(textField3, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    JTextField textField3 = new JTextField();

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent comp) {
            boolean returnValue;
            JTextField textField = (JTextField) comp;
            try {
                Integer.parseInt(textField.getText());
                returnValue = true;/*from  ww  w. j  av a 2s  . c  o m*/
            } catch (NumberFormatException e) {
                returnValue = false;
            }
            return returnValue;
        }
    };

    textField1.setInputVerifier(verifier);
    textField3.setInputVerifier(verifier);

    Container contentPane = frame.getContentPane();
    contentPane.add(textField1, BorderLayout.NORTH);
    contentPane.add(textField2, BorderLayout.CENTER);
    contentPane.add(textField3, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:AddingActionCommandActionListenerSample.java

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

    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.length() != 0) && !(text.equals("Exit"))) {
                JOptionPane.showMessageDialog(frame, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }//from   w ww . ja  v  a2 s.  c o m
        }
    };
    textField.setInputVerifier(verifier);

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

From source file:MainClass.java

public static void main(String argv[]) {

    java.net.URL u = null;// ww w  .j a va 2 s .  c om
    try {
        u = new java.net.URL("http://www.java2s.com/");
    } catch (java.net.MalformedURLException ignored) {
    }

    JFormattedTextField ftf1 = new JFormattedTextField(u);
    JFormattedTextField ftf2 = new JFormattedTextField(u);

    ftf2.setInputVerifier(new InputVerifier() {
        public boolean verify(JComponent input) {
            if (!(input instanceof JFormattedTextField))
                return true;
            return ((JFormattedTextField) input).isEditValid();
        }
    });

    JPanel p = new JPanel(new GridLayout(0, 2, 3, 8));
    p.add(new JLabel("plain JFormattedTextField:"));
    p.add(ftf1);
    p.add(new JLabel("FTF with InputVerifier:"));
    p.add(ftf2);
    p.add(new JLabel("plain JTextField:"));
    p.add(new JTextField(u.toString()));

    JFrame f = new JFrame("MainClass");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JLabel("Try to delete the colon in each field."), "North");
    f.getContentPane().add(p, "Center");
    f.pack();
    f.setVisible(true);
}

From source file:MainClass.java

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

    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.length() != 0) && !(text.equals("Exit"))) {
                JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog",
                        JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }// ww  w.  ja va  2s .  c om
        }
    };
    nameTextField.setInputVerifier(verifier);

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

From source file:FtfInputVerifier.java

public static void main(String argv[]) {

    java.net.URL u = null;/* w w w  .  j  a v a2 s . c o m*/
    try {
        u = new java.net.URL("http://www.ora.com/");
    } catch (java.net.MalformedURLException ignored) {
    }

    // create two identical JFormattedTextFields
    JFormattedTextField ftf1 = new JFormattedTextField(u);
    JFormattedTextField ftf2 = new JFormattedTextField(u);

    // and set an InputVerifier on one of them
    ftf2.setInputVerifier(new InputVerifier() {
        public boolean verify(JComponent input) {
            if (!(input instanceof JFormattedTextField))
                return true; // give up focus
            return ((JFormattedTextField) input).isEditValid();
        }
    });

    JPanel p = new JPanel(new java.awt.GridLayout(0, 2, 3, 8));
    p.add(new JLabel("plain JFormattedTextField:", JLabel.RIGHT));
    p.add(ftf1);
    p.add(new JLabel("FTF with InputVerifier:", JLabel.RIGHT));
    p.add(ftf2);
    p.add(new JLabel("plain JTextField:", JLabel.RIGHT));
    p.add(new JTextField(u.toString()));
    p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    JFrame f = new JFrame("FtfInputVerifier");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JLabel("Try to delete the colon in each field.", JLabel.CENTER),
            java.awt.BorderLayout.NORTH);
    f.getContentPane().add(p, java.awt.BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
}

From source file:JTextFieldSample.java

public static void main(String args[]) {
    String title = (args.length == 0 ? "TextField Listener Example" : args[0]);
    JFrame frame = new JFrame(title);
    Container content = frame.getContentPane();

    JPanel namePanel = new JPanel(new BorderLayout());
    JLabel nameLabel = new JLabel("Name: ");
    nameLabel.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField nameTextField = new JTextField();
    nameLabel.setLabelFor(nameTextField);
    namePanel.add(nameLabel, BorderLayout.WEST);
    namePanel.add(nameTextField, BorderLayout.CENTER);
    content.add(namePanel, BorderLayout.NORTH);

    JPanel cityPanel = new JPanel(new BorderLayout());
    JLabel cityLabel = new JLabel("City: ");
    cityLabel.setDisplayedMnemonic(KeyEvent.VK_C);
    JTextField cityTextField = new JTextField();
    cityLabel.setLabelFor(cityTextField);
    cityPanel.add(cityLabel, BorderLayout.WEST);
    cityPanel.add(cityTextField, BorderLayout.CENTER);
    content.add(cityPanel, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
        }/* w  w  w . j ava2s. c om*/
    };
    nameTextField.setActionCommand("Yo");
    nameTextField.addActionListener(actionListener);
    cityTextField.addActionListener(actionListener);

    KeyListener keyListener = new KeyListener() {
        public void keyPressed(KeyEvent keyEvent) {
            printIt("Pressed", keyEvent);
        }

        public void keyReleased(KeyEvent keyEvent) {
            printIt("Released", keyEvent);
        }

        public void keyTyped(KeyEvent keyEvent) {
            printIt("Typed", keyEvent);
        }

        private void printIt(String title, KeyEvent keyEvent) {
            int keyCode = keyEvent.getKeyCode();
            String keyText = KeyEvent.getKeyText(keyCode);
            System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar());
        }
    };
    nameTextField.addKeyListener(keyListener);
    cityTextField.addKeyListener(keyListener);

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.length() != 0) && !(text.equals("Exit"))) {
                Runnable runnable = new Runnable() {
                    public void run() {
                        JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog",
                                JOptionPane.ERROR_MESSAGE);
                    }
                };
                SwingUtilities.invokeLater(runnable);
                return false;
            } else {
                return true;
            }
        }
    };
    nameTextField.setInputVerifier(verifier);
    cityTextField.setInputVerifier(verifier);

    DocumentListener documentListener = new DocumentListener() {
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            String typeString = null;
            if (type.equals(DocumentEvent.EventType.CHANGE)) {
                typeString = "Change";
            } else if (type.equals(DocumentEvent.EventType.INSERT)) {
                typeString = "Insert";
            } else if (type.equals(DocumentEvent.EventType.REMOVE)) {
                typeString = "Remove";
            }
            System.out.print("Type  :   " + typeString + " / ");
            Document source = documentEvent.getDocument();
            int length = source.getLength();
            try {
                System.out.println("Contents: " + source.getText(0, length));
            } catch (BadLocationException badLocationException) {
                System.out.println("Contents: Unknown");
            }
        }
    };
    nameTextField.getDocument().addDocumentListener(documentListener);
    cityTextField.getDocument().addDocumentListener(documentListener);

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

From source file:Main.java

private InputVerifier getInputVerifier() {
    InputVerifier verifier = new InputVerifier() {
        @Override/*from  ww w . j av a  2s. c o m*/
        public boolean verify(JComponent input) {
            JFormattedTextField field = (JFormattedTextField) input;
            String text = field.getText();
            return isValidDate(text);
        }

        @Override
        public boolean shouldYieldFocus(JComponent input) {
            boolean valid = verify(input);
            if (!valid) {
                JOptionPane.showMessageDialog(null, "Please enter a valid date in format dd/mm/yyyy");
            }
            return valid;
        }
    };
    return verifier;
}