getName method cannot verify JTextField from other class

203 Views Asked by At

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?

2

There are 2 best solutions below

2
Bahramdun Adil On

Here in you code this statement if(text.equals("") && name.equals("tfAddress")) will never satisfied, because of if(text.trim().length() == 0 || text.equals("")) check, so text.equals("") will never return true so name.equals("tfAddress") will skip.

In the first check of if clause if the text is empty, then the code will return. So here if(text.equals("") && name.equals("tfAddress")) you can check for if(name.equals("tfAddress"))

0
Francisunoxx On

I just solved the problem. I made a mistake on the logic. I based on the text.trim().length() == 0 || text.equals("") so when I the program runs It check first if text is empty. What I did I set the condition based on the setName method. Hoping this will help to others.

private void validateJTextField()
{
    tfLastName.setName("tfLastName");
    tfFirstName.setName("tfFirstName");
    tfMiddleName.setName("tfMiddleName");
    tfNickname.setName("tfNickname");
    tfAddress.setName("tfAddress");
    tfContact.setName("tfContact");
    tfLastName.setInputVerifier(new Validation());
    tfFirstName.setInputVerifier(new Validation());
    tfMiddleName.setInputVerifier(new Validation());
    tfNickname.setInputVerifier(new Validation());
    tfAddress.setInputVerifier(new Validation());
    tfContact.setInputVerifier(new Validation());
}

public class Validation extends InputVerifier
{  
@Override
public boolean verify(JComponent input) 
{
    String text = null;
    String cb = null;
    String name = input.getName();
    if(input instanceof JTextField)
    {
        text = ((JTextField) input).getText();

        if(name.equals("tfLastName") || name.equals("tfFirstName") || name.equals("tfMiddleName") || name.equals("tfNickname"))
        {
            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)
                {

                }
            }
        }
        else if(name.equals("tfAddress"))
        {
            if(text.trim().length() == 0 || text.equals(""))
            {
                JOptionPane.showMessageDialog(null, "Cannot left blank");
                return false;//Return false if the component need to keep focus
            }
        }
}