0

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?

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • What is the logic you want to achieve with your code? How did you test it, what was the text of the control? Was validateJTextField called? If so, where and how? – Lajos Arpad Sep 27 '16 at 08:07
  • @LajosArpad Arpad I just wanna to verify if the `JTextFields` is equals to `"tfAddress"` if it's true it can accept numeric and characters. Yes `validateJTextField` was called on the constructor. – Francisunoxx Sep 27 '16 at 08:13
  • Francis, if you debug your code and go to the line where you think your problem is what is the value of text and what is the value of name in the critical case? – Lajos Arpad Sep 27 '16 at 08:24
  • @LajosArpad The null was already gone. But cannot satisfy my condition. I'm not sure if `getName()` was getting the correct JTextField. – Francisunoxx Sep 27 '16 at 08:34
  • 1
    Francis, I wish you good luck. I will navigate to questions where the op gives enough information to solve his/her problem. – Lajos Arpad Sep 27 '16 at 08:43
  • Please create a valid [mcve] if you're still stuck and need help. – Hovercraft Full Of Eels Sep 27 '16 at 11:27

2 Answers2

0

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"))

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
0

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
            }
        }
}
Francisunoxx
  • 1,440
  • 3
  • 22
  • 45