从另一个类禁用JButton

时间:2016-10-24 06:15:00

标签: java swing jbutton

我尝试在同时使用输入验证程序时禁用按钮。我希望在textfields为空时禁用按钮。以下是我的表现方式。

AddEmployee.class

public class AddEmployee extends javax.swing.JInternalFrame
{
    public JButton getJButton()
    {
        return addEmployeeButton;
    }

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

ValidateComponents.class

public class ValidateComponents extends InputVerifier
{
   public void disable(JButton name)
   {
       AddEmployee employee = new AddEmployee();
       employee.setJButton(name);
       name.setEnabled(false);
   }

@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"))
        {
            if(tf.trim().length() == 0 || tf.equals(""))
            {
                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
            }
       }
   }
}

在if条件下调用disable()方法是否有效?因此,如果textfields为空,它可以调用。任何帮助将不胜感激!谢谢:))

更新

public class ValidateComponents extends InputVerifier
{
      private AddEmployee employee = new AddEmployee();
      JButton myButton;
      public void disable(JButton name, boolean disable)
      {
        employee.setJButton(name);
        name.setEnabled(!disable);
        myButton = name;
      }

@Override
public boolean verify(JComponent input) 
{
    String tf = 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);
            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
            }
}
}
}

1 个答案:

答案 0 :(得分:2)

你的课程应该稍微修改一下才能更灵活

private AddEmployee employee = new AddEmployee();

public void disable(JButton name, boolean disable){
    employee.setJButton(name);
    name.setEnabled(!disable); //NOT because yours is called disable and Swing's is called enabled ;)
}

由此,您可以轻松更改按钮的状态。问题是,为什么需要将其添加到AddEmployee?使用验证程序创建一个布尔值(每个示例有效),它将告诉是否应启用按钮。如果不这样做,您将永远不会重新激活该按钮。

if(name.equals("tfLastName") || name.equals("tfFirstName") || name.equals("tfMiddleName") || name.equals("tfNickname") || name.equals("tfGuardianContactName"))
        {
            boolean valid = tf.trim().length() > 0; //Change the sign here and no need for the following > || tf.equals("")
            disable(myButton, !valid); // YOU NEED TO HAVE THE BUTTON INSTANCE SOMEWHERE, IN THE CONSTRUCTOR OR A SETTER...
            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 {
                // ADD THE VALID CASE HERE
            }
       }

请注意,您应该将要检查的实例发送给验证程序并将其存储在集合中,然后存储到input,检查集合中是否有相同的实例(比如列表) ,这比检查名称要干净;)

public class ValidateComponents extends InputVerifier
{
    JButton myButton;

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

    ...

}

您只需在创建ValidateComponents实例时设置按钮。