JTextField
他の使用InputVerifier
方法を確認したかったのです。私がしたことJTextField
を使用して別の名前を設定しましたsetName
。
private void validateJTextField()
{
tfAddress.setName("tfAddress");
tfLastName.setInputVerifier(new Validation());
tfFirstName.setInputVerifier(new Validation());
tfMiddleName.setInputVerifier(new Validation());
tfNickname.setInputVerifier(new Validation());
tfAddress.setInputVerifier(new Validation());
}
検証クラス
public class Validation extends InputVerifier
{
@Override
public boolean verify(JComponent input)
{
String text = null;
String name = input.getName();
if(input instanceof JTextField)
{
text = ((JTextField) input).getText();
if(text.trim().length() == 0 || text.equals(""))
{
JOptionPane.showMessageDialog(null, "Cannot left blank");
return false;//Return false if the component need to keep focus
}
else
{
try
{
Double.parseDouble(text);
JOptionPane.showMessageDialog(null, "Cannot insert numeric");
return false;
}
catch(NumberFormatException e)
{
}
}
if(text.equals("") && name.equals("tfAddress"))
{
System.out.print("This is tfAddress");
return false;
}
}
return true;//Return true if the component should give up focus
}
}
ここでわかるように、name
String が等しいかどうかを検証または確認しようとして"tfAddress"
いますが、残念ながら条件を満たしていません。どうすればこれを解決できますか?