我将输入验证器设置为我的两个文本字段,并且我希望我的单选按钮应该处于非活动状态,直到两个文本字段被验证。
(在文本字段验证之前,用户不能从单选按钮中选择任何内容。)
在验证 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)