[英]Disable JButton from another class
I'm trying to disabling button while using Input Verifier at the same time. 我试图在同时使用Input Verifier时禁用按钮。 I wanted to disable button when textfields are empty.
我想在文本字段为空时禁用按钮。 Here's how I do it.
这是我的方法。
AddEmployee.class AddEmployee.class
public class AddEmployee extends javax.swing.JInternalFrame
{
public JButton getJButton()
{
return addEmployeeButton;
}
public void setJButton(JButton buttonObject)
{
buttonObject = addEmployeeButton;
}
}
ValidateComponents.class 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
}
}
}
}
Is it valid to call the disable()
method under my if condition? 在if条件下调用
disable()
方法是否有效? So it can call if textfields are empty. 因此,如果文本字段为空,它可以调用。 Any help would appreciated!
任何帮助,将不胜感激! Thanks :)
谢谢 :)
Update: 更新:
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
}
}
}
}
Your class should be modify just a bit to be more flexible 您的班级应该稍作修改以更加灵活
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 ;)
}
From this, you can easily change the status of a button. 由此,您可以轻松更改按钮的状态。 The question is, why do you need to add this to
AddEmployee
? 问题是,为什么需要将此添加到
AddEmployee
? Use you verifier to create a boolean (valid per example) that will tell if a button should or not be enabled. 使用验证程序创建一个布尔值(每个示例有效),该布尔值将告诉您是否应该启用按钮。 If you don't, you will never reactivate the button.
如果您不这样做,则永远不会重新激活该按钮。
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
}
}
Just a note, you should send the instance you want to check to your verifier and store those in a Collection, then for an input
, check if you have the same instance in the collection (say a List), this will be cleaner than checking the names ;) 仅需注意,您应该将要检查的实例发送到验证程序并将其存储在Collection中,然后作为
input
,检查集合中是否有相同的实例(例如List),这比检查要干净名字 ;)
public class ValidateComponents extends InputVerifier
{
JButton myButton;
public ValidateComponents(JButton button)
this.myButton = button;
}
...
}
You just need to set the button when you create your instance of ValidateComponents. 创建ValidateComponents实例时,只需设置按钮即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.