I wanted to verify my other JTextField using InputVerifier method. What I did I set a named for a different JTextField using 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());
}
Validation class
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
}
}
As you can see here I'm trying to validate or check if name String is equals to "tfAddress" but unfortunately it won't met the condition. Any help or tips how can I solve this?
Here in you code this statement
if(text.equals("") && name.equals("tfAddress"))will never satisfied, because ofif(text.trim().length() == 0 || text.equals(""))check, sotext.equals("")will never return true soname.equals("tfAddress")will skip.In the first check of
ifclause if the text is empty, then the code will return. So hereif(text.equals("") && name.equals("tfAddress"))you can check forif(name.equals("tfAddress"))