0

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();
Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • 1
    *"I can't give a values as `null` because this will throw me a `NullPointerException`."* The method/code receiving the result could just check for `null` before further processing. – Andrew Thompson Oct 25 '16 at 11:02
  • 1
    *"Can somebody tell me what is the best way solving this issue?"* If you cannot get 'checking for `null` to work for this case, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Oct 25 '16 at 11:03
  • I don't get your `disable(...)` method, especially why you are setting the button in the frame again. Why don't you just call `myButton.setEnabled(!valid)` in `verify(...)`? Also `setJButton(JButton buttonObject)` seems to have a wrong body. Try to make the parameter `final` and see what happens to see what I mea ;). – Thomas Oct 25 '16 at 11:20
  • 1
    Besides my comment above, when you tell us you get errors like `StackOverFlowError` you should always include a stacktrace and mark the relevant lines (we can't/won't count lines to get the correct numbers). – Thomas Oct 25 '16 at 11:24
  • @Thomas, ‘can’t’ is the correct term. There are always imports and other stuff that makes it impossible to guess where to start counting. – Ole V.V. Oct 25 '16 at 12:02
  • @OleV.V. in most cases yes, I added "won't" for the cases that contain everything - I still wouldn't want to count lines. – Thomas Oct 25 '16 at 12:21
  • @Thomas It gives me StackOverFlow error when I tried to create a object of `AddEmployee` in the `ValidateComponents` constructor. Any reason why? – Francisunoxx Oct 25 '16 at 12:23
  • As I said you should post a stacktrace. Without that it's hard to say since you also didn't post your exact code (at least it seems so) - and we might want even want you to post all your code if it is very much, just the relevant parts and the stack trace might help identify those. – Thomas Oct 25 '16 at 14:29

0 Answers0