F
Fencer
Hello, I have a button (JButton) in a dialog (JDialog) that should be
disabled if there's nothing entered in a particular textfield.
I solved that by creating this little class that implements the
DocumentListener interface:
class ButtonEnablerDisabler implements DocumentListener {
ButtonEnablerDisabler(JButton button) {
this.button = button;
}
public void changedUpdate(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
public void insertUpdate(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
public void removeUpdate(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
private JButton button;
}
and then I simply do:
myTextField.getDocument().addDocumentListener(new
ButtonEnablerDisabler(myButton));
it seems to work fine. Now my problem is this...I actually have four
text fields and the button shouldn't be enabled unless there's text in
all four.
My first attempt was silly: I simply repeated the line of code above for
all text fields, but that doesn't work because then you can enable the
button even if there's no text in the other fields. How should I solve
this problem?
- Fencer
disabled if there's nothing entered in a particular textfield.
I solved that by creating this little class that implements the
DocumentListener interface:
class ButtonEnablerDisabler implements DocumentListener {
ButtonEnablerDisabler(JButton button) {
this.button = button;
}
public void changedUpdate(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
public void insertUpdate(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
public void removeUpdate(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
private JButton button;
}
and then I simply do:
myTextField.getDocument().addDocumentListener(new
ButtonEnablerDisabler(myButton));
it seems to work fine. Now my problem is this...I actually have four
text fields and the button shouldn't be enabled unless there's text in
all four.
My first attempt was silly: I simply repeated the line of code above for
all text fields, but that doesn't work because then you can enable the
button even if there's no text in the other fields. How should I solve
this problem?
- Fencer