[英]Validating InputVerifier from GUI Builder
I created some JTextFields
and JComboBox
using Gui Builder(Drag and drop) and added a submit trigger JButton
. 我使用Gui Builder(拖放)创建了一些
JTextFields
和JComboBox
,并添加了一个提交触发器JButton
。 I wanted to validate the Swing Controls by checking if textfields are empty calling the method verify()
in my GUI. 我想通过检查文本字段是否为空来验证Swing控件,方法是在GUI中调用
verify()
方法。
validation class 验证类
public class validation extends InputVerifier
{
@Override
public boolean verify(JComponent input)
{
String text = null;
String cb = null;
if(input instanceof JTextField)
{
text = ((JTextField) input).getText();
}
else if(input instanceof JComboBox)
{
cb = ((JComboBox) input).getSelectedItem().toString();
}
return true;
}
}
AddEmployee 添加员工
I already created a instance of my class validation. 我已经创建了类验证的实例。
public class AddEmployee extends javax.swing.JInternalFrame{
/**
* Creates new form AddEmployee
*/
validation v = new validation();
private void addButtonActionPerformed(java.awt.event.ActionEvent evt){
String lastName = tfLastName.getText();
String firstName = tfFirstName.getText();
tfLastName.setName("lastName");
tfFirstName.setName("firstName");
v.verify(tfLastName,tfFirstName);
}
}//end of AddEmployee
When I'm trying to passed the variable names of my JTextFields
in verify()
method it throws me a error. 当我尝试在
verify()
方法中传递JTextFields
的变量名时,将引发错误。
no suitable method found for verify(JTextFields,JTextFields) method InputVerifier.verify is not applicable
找不到适用于verify(JTextFields,JTextFields)方法InputVerifier.verify的合适方法
What I did I created a void method verify()
under validation class. 我在验证类下创建了一个无效方法
verify()
。
public void verify(JTextField tfLastName, JTextField tfFirstName) {
}
Question 题
Any help or tips I would greatly appreciated! 任何帮助或提示,我将不胜感激!
Best practice: when you create the JTextField
assign the verifier to it: 最佳实践:创建
JTextField
将验证器分配给它:
tfLastName.setInputVerifier(new validation());
BTW: Use correct java naming convention: V alidation 顺便说一句:使用正确的Java命名约定: V alidation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.