0

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)
Sajad
  • 2,273
  • 11
  • 49
  • 92
  • @AndrewThompson My question is that how can i do it?! – Sajad Oct 05 '13 at 15:49
  • @AndrewThompson What is your mean about `edit the question into ..the question` ? – Sajad Oct 05 '13 at 16:08
  • @AndrewThompson I know that how edit a post, You say that edit question title or body? – Sajad Oct 05 '13 at 16:16
  • I had not noticed the edit 8 minutes ago that added a question. That is enough. (As to 'title/body' for question. If I see a question in a title and am editing the question, I'll turn it into a statement and repeat the question in the first line of the post body. That is just personal preference coming through though, not an SE/SO 'rule or guideline'.) – Andrew Thompson Oct 05 '13 at 16:19

2 Answers2

1

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.

Sage
  • 15,290
  • 3
  • 33
  • 38
  • It works, But when want to select in one of radio buttons in inactive mode, the `java.lang.NullPointerException` occur. – Sajad Oct 05 '13 at 15:48
  • @Sajjad Post the Stacktrace so we can see where occur the error and what is the cause – Omar Mainegra Oct 05 '13 at 15:59
  • @Sajjad, I have updated the answer. Check with the current change and avoid adding input verifier to JRadioButton. It won't be necessary, I think. And if still the exception pursue, plz post the stacktrace as *@omainegra* suggested. – Sage Oct 05 '13 at 16:03
  • @omainegra I post it. – Sajad Oct 05 '13 at 16:13
  • @Sage i commented them, But still that exception occur. – Sajad Oct 05 '13 at 16:14
  • yes updated. check the code part i have attached at the end of my answer. It is working now as expected without any error. – Sage Oct 05 '13 at 16:38
  • ah, I have just noticed. :) – Sage Oct 05 '13 at 16:40
1

I Think the better approach is to use data binding. Check JGoodies Binding at http://www.jgoodies.com/freeware/libraries/binding/ but you can try this.

Modify your class MyVerifier adding a constructor that accept and array of JComponent. This components will be enabled if validation is true, disabled otherwise.

public class MyVerifier extends InputVerifier {
    JComponent [] components;

    public MyVerifier(){
    }

    public MyVerifier(JComponent [] components){
        this.components = components;
    }

    private void enableComponents(){
        //enable dependent components
        if (components != null){
            for (JComponent r: components){
                r.setEnabled(true);
            }
        }
    }

    private void disableComponents(){
        //disable dependent components
        if (components != null){
            for (JComponent r: components){
                r.setEnabled(false);
            }
        }
    }

    @Override
    public boolean verify(JComponent input) {
        String name = input.getName();

        if (name == null) {                
            return false;
        }
        else if (name.equals("fntf")) {
            String text = ((JTextField) input).getText().trim();
            if (text.matches(".*\\d.*")){

                //disable dependent components
                disableComponents();

                return false; // Have Digit
            }            
        } else if (name.equals("lntf")) {
            String text = ((JTextField) input).getText();
            if (text.matches(".*\\d.*")){
                //disable dependent components
                disableComponents();

                return false;
            }
        }

        enableComponents();

        return true;
    }
}

Then construct your validator passing the list components you want to enable or disabled depending on the result of the validation.

fNameTf.setInputVerifier(new MyVerifier(new JComponent[]{maleRb, femaleRb}));

This will work not only for JRadioButton but for any JComponent

Omar Mainegra
  • 4,006
  • 22
  • 30