javaswingvalidationjtextfieldinputverifier

Set different inputVerifier for JTextFields


Here i set InputVerifier to my text field (tf1) :

public class MyInputVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText().trim();
    if (text.isEmpty()) return false;
    if (text.matches(".*\\d.*")) return false;

    return true;
}

public static class Tester extends JFrame implements ActionListener {
    JTextField tf1,tf2;
    JButton okBtn;

    public Tester() {
        add(panel(), BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Tester();
            }
        });
    }

    public JPanel panel() {
        JPanel panel = new JPanel();
        okBtn = new JButton("Ok");
        okBtn.addActionListener(this);
        tf1 = new JTextField(10);
        tf2 = new JTextField(10);
        tf1.setInputVerifier(new MyInputVerifier());
        tf2.setInputVerifier(new MyInputVerifier());
        panel.add(tf1);
        panel.add(tf2);
        panel.add(okBtn);
        return panel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        MyInputVerifier inputVerifier = new MyInputVerifier();
        if (e.getSource() == okBtn) {
            if (inputVerifier.verify(tf1)){
                JOptionPane.showMessageDialog(null, "True Value");
            }
            else JOptionPane.showMessageDialog(null, "False Value");
        }
    }
}
}

Now i want to set input verifier to my second text field (tf2) , But my tf2 verifier condition is different than tf1.

For example my verify() of tf2 should be like:

@Override
public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText().trim();
    if (text.equalsIgnoreCase("Me")) return true;
    return true;
}

And so on for more text fields.

How to verify two or more text fields with different conditions with one extended class?


Solution

  • How to verify two or more text fields with different conditions with one extended class?

    You need to identify JTextField with a name:

    JTextField jTextField = new JTextField();
    jTextField.setName("tf1");
    

    And then use it like ths:

       public boolean verify(JComponent input) {
            String name = input.getName();
    
            if(name.equals("tf1"))
            {
                String text = ((JTextField) input).getText().trim();
                if (text.isEmpty()) return false;
                if (text.matches(".*\\d.*")) return false;
            }
            if(name.equals("tf2"))
            {
                //other condition
                return true;
            }
    
            return true;
        }
    

    In addition I think you need to check this:

    The purpose of this class is to help clients support smooth focus navigation through GUIs with text fields