I have a Java application that has a form with 4 text fields and a button. I was wondering if there is a way for the program to be constantly checking the fields using .isEmpty()
to see if there has been an input yet, and once there is an input in all fields, the button will become available to click?
Add a DocumentListener.
JButton button = new JButton("Button");
JTextField field = new JTextField();
field.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void changedUpdate(DocumentEvent arg0) {
if(field.getText().isEmpty()){
button.setEnabled(true);
}
}
@Override
public void insertUpdate(DocumentEvent arg0) {
if(field.getText().isEmpty()){
button.setEnabled(true);
}
}
@Override
public void removeUpdate(DocumentEvent arg0) {
if(field.getText().isEmpty()){
button.setEnabled(true);
}
}
});
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.