在文本字段和单选按钮之间输入验证程序

时间:2013-10-04 19:02:17

标签: java swing jtextfield jradiobutton inputverifier

我将输入验证程序设置为我的两个文本字段,我希望我的单选按钮应该处于非活动状态,直到两个文本字段被验证。

(用户不应从单选按钮中选择任何内容,直到文本字段验证。)

在JTextFields验证之前,JRadioButtons的非活动状态如何?

我的代码:

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;
    }
}
}

更新

我尝试@omainegra解决方案并发生此异常:

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)

2 个答案:

答案 0 :(得分:2)

从您的代码中我猜public class MyVerifierAddUserDialog2的内部类。如果是这样,那么您应该能够在开始和结束时禁用JRadioButton功能中的boolean verify(JComponent input)。这是我能想到的最简单的解决方案:

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;
    }

但这样做,恐怕你不会拦截禁用效果。 在您的代码中:

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());

您无需为常见输入验证创建4个对象。只需创建一个并将其传递给这四个输入对象。 再次 ,当您将“男性”传递给JRadioButton时,它会设置文本字段而不是名称。所以你需要使用maleRb.setName("male")femaleRb.setName("female"),错误就会消失。

答案 1 :(得分:1)

我认为更好的方法是使用数据绑定。在http://www.jgoodies.com/freeware/libraries/binding/查看 JGoodies Binding ,但您可以尝试一下。

修改您的类MyVerifier,添加一个接受JComponent数组的构造函数。如果验证为真,则启用此组件,否则禁用。

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;
    }
}

然后构建验证器,传递要启用或禁用的列表组件,具体取决于验证结果。

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

这不仅适用于JRadioButton,也适用于任何JComponent