Francisunoxx
Francisunoxx

Reputation: 1458

Passing class instance in the void method

I'm trying to control button from Add Employee class so I can disable or enable it if the user input is accepted or not using InputVerifier. What I did I made a getter and setter for my addEmployeeButton

public class AddEmployee extends javax.swing.JInternalFrame{

   public JButton getJButton()
   {
       return addEmployeeButton;
   }

   public void setJButton(JButton buttonObject)
   {
       buttonObject = addEmployeeButton;
   }

   private void registerJComponents()
   {   
    //INPUT VERIFIER
    tfLastName.setInputVerifier(new ValidateComponents(addEmployeeButton));
    tfFirstName.setInputVerifier(new ValidateComponents(addEmployeeButton));
    tfMiddleName.setInputVerifier(new ValidateComponents(addEmployeeButton));
    tfNickname.setInputVerifier(new ValidateComponents(addEmployeeButton));
    taAddress.setInputVerifier(new ValidateComponents(addEmployeeButton));
    tfContact.setInputVerifier(new ValidateComponents(addEmployeeButton));
    tfContactName.setInputVerifier(new ValidateComponents(addEmployeeButton));
    tfContactNo.setInputVerifier(new ValidateComponents(addEmployeeButton));     
   }

}

I'm accessing it from the another class named ValidateComponents. But the disable method is expecting to passed a AddEmployee instance. I can't give a values as null because this will throw me a NullPointerException.

public class ValidateComponents extends InputVerifier
{
JButton myButton;

public ValidateComponents(JButton button)
{
    this.myButton = button;
}

public void disable(JButton name, boolean disable, AddEmployee employee)
{
    employee.setJButton(name);
    name.setEnabled(!disable);
}

@Override
public boolean verify(JComponent input) 
{
    String tf = null;
    //String ta = null;
    String name = input.getName(); //GETTING THE NAME OF THE COMPONENT
    if(input instanceof JTextField)
    {
        tf = ((JTextField) input).getText();

        if(name.equals("tfLastName") || name.equals("tfFirstName") || name.equals("tfMiddleName") || name.equals("tfNickname") || name.equals("tfGuardianContactName"))
        {
            boolean valid = tf.trim().length() > 0;

            disable(myButton, !valid);//Button instance
            if(!valid)
            {
                input.setBackground(Color.PINK);//Set background color to pink if false.
                input.setToolTipText("Fields cannot left blank");
                return false;//Return false if the component need to keep focus
            }
            else
            {

            }
        }   
    }
    input.setBackground(Color.WHITE);//Set backgroud color to white if true.
    return true;//Return true if the component should give up focus
}
}

2nd try:

When I tried to create a instance of AddEmployee class outside the method. It gives me a StackOverFlowError and this pointing to my object that created. Can somebody tell me what is the best way solving this issue? Any help would greatly appreciated.

private AddEmployee employee = new AddEmployee();

Upvotes: 0

Views: 58

Answers (0)

Related Questions