Search code examples
javaswingjtextfieldjradiobuttoninputverifier

Input verifier between textfields and radio buttons


I set input verifier to my two text fields and i want that my radio buttons should be Inactive until two text fields being verified.

(User should can not select any from radio button, until text fields verify.)

How inactive JRadioButtons until JTextFields be verify?

My code:

public class AddUserDialog2 extends JDialog implements ActionListener {
JButton cancelBtn, okBtn;
JTextField fNameTf, lNameTf;
JRadioButton maleRb, femaleRb;
ButtonGroup group;
JLabel fNameLbl, lNameLbl, genderLbl, fNameTemp, lNameTemp, tempBtn, temp3;

public AddUserDialog2() {
    add(createForm(), BorderLayout.CENTER);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    pack();
    setVisible(true);
}

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

public JPanel createForm() {
    JPanel panel = new JPanel();
    okBtn = new JButton("Ok");
    cancelBtn = new JButton("Cancel");
    tempBtn = new JLabel();
    fNameLbl = new JLabel("First Name");
    fNameTemp = new JLabel();
    lNameLbl = new JLabel("Last Name");
    lNameTemp = new JLabel();
    genderLbl = new JLabel("Gender");
    fNameTf = new JTextField(10);
    fNameTf.setName("fntf");
    fNameTf.setInputVerifier(new MyVerifier());
    lNameTf = new JTextField(10);
    lNameTf.setName("lntf");
    lNameTf.setInputVerifier(new MyVerifier());
    maleRb = new JRadioButton("Male");
    maleRb.setInputVerifier(new MyVerifier());
    femaleRb = new JRadioButton("Female");
    femaleRb.setInputVerifier(new MyVerifier());
    temp3 = new JLabel();
    group = new ButtonGroup();
    group.add(maleRb);
    group.add(femaleRb);
    maleRb.addActionListener(this);
    femaleRb.addActionListener(this);
    panel.add(fNameLbl);
    panel.add(fNameTf);
    panel.add(fNameTemp);
    panel.add(lNameLbl);
    panel.add(lNameTf);
    panel.add(lNameTemp);
    panel.add(genderLbl);
    JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    radioPanel.add(maleRb);
    radioPanel.add(femaleRb);
    panel.add(radioPanel);
    panel.add(temp3);
    panel.add(okBtn);
    okBtn.addActionListener(this);
    panel.add(cancelBtn);
    cancelBtn.addActionListener(this);
    panel.add(tempBtn);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 10, 80, 60);
    return panel;
}

@Override
public void actionPerformed(ActionEvent e) {
    // Be inactive until two text field verified.
}

public class MyVerifier extends InputVerifier {

    @Override
    public boolean verify(JComponent input) {
        String name = input.getName();
        if (name.equals("fntf")) {
            String text = ((JTextField) input).getText().trim();
            if (text.matches(".*\\d.*")) return false;       // Have Digit
        } else if (name.equals("lntf")) {
            String text = ((JTextField) input).getText();
            if (text.matches(".*\\d.*")) return false;
        }
        return true;
    }
}
}

Update

I try @omainegra solution and this exception occur:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Project.AddUserDialog2$MyVerifier.verify(AddUserDialog2.java:107)
at javax.swing.InputVerifier.shouldYieldFocus(InputVerifier.java:132)
at javax.swing.JComponent$1.acceptRequestFocus(JComponent.java:3589)
at java.awt.Component.isRequestFocusAccepted(Component.java:7728)
at java.awt.Component.requestFocusHelper(Component.java:7610)
at java.awt.Component.requestFocusHelper(Component.java:7603)
at java.awt.Component.requestFocus(Component.java:7411)
at javax.swing.JComponent.requestFocus(JComponent.java:1476)
at javax.swing.text.DefaultCaret.adjustFocus(DefaultCaret.java:530)
at javax.swing.text.DefaultCaret.adjustCaretAndFocus(DefaultCaret.java:503)
at javax.swing.text.DefaultCaret.mousePressed(DefaultCaret.java:492)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
at java.awt.Component.processMouseEvent(Component.java:6502)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Solution

  • From your code i guess public class MyVerifier is an inner class of AddUserDialog2. If so then you should be able to disable JRadioButton inside the boolean verify(JComponent input) function, at the beginning and at the end. This is the easiest solution i can think of:

    public boolean verify(JComponent input) {
    
            maleRb.setEnabled(false);
            femaleRb.setEnabled(false);
    
            String name = input.getName();
            if (name.equals("fntf")) {
                String text = ((JTextField) input).getText().trim();
                if (text.matches(".*\\d.*")) return false;       // Have Digit
            } else if (name.equals("lntf")) {
                String text = ((JTextField) input).getText();
                if (text.matches(".*\\d.*")) return false;
            }
    
            maleRb.setEnabled(true);
            femaleRb.setEnabled(true);
            return true;
        }
    

    But doing so, I am afraid you won't intercept the disable effect. In your code:

    fNameTf.setName("fntf");
        fNameTf.setInputVerifier(new MyVerifier());
        lNameTf = new JTextField(10);
        lNameTf.setName("lntf");
        lNameTf.setInputVerifier(new MyVerifier());
        maleRb = new JRadioButton("Male");
        maleRb.setInputVerifier(new MyVerifier());
        maleRb.setName("male");
        femaleRb = new JRadioButton("Female");
        femaleRb.setName("Female");
        femaleRb.setInputVerifier(new MyVerifier());
    

    you don't need to create 4 objects for common input verification. Just create one and pass it to these four input object. Again, when you are passing "male" to JRadioButton it is setting it's text feild but not name. so you need to use maleRb.setName("male") and femaleRb.setName("female") and the error will disappear.