I'm trying to disabling button while using Input Verifier at the same time. I wanted to disable button when textfields are empty. Here's how I do it.
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
}
}
}
}
Is it valid to call the disable()
method under my if condition? 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
? 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 ;)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.