Document: addDocumentListener(DocumentListener listener)
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class Main {
JButton button = new JButton("foo");
JTextField textfield = new JTextField(10);
Document document;
public Main() {
document = textfield.getDocument();
document.addDocumentListener(new JButtonStateController());
}
class JButtonStateController implements DocumentListener {
JButtonStateController() {
}
public void changedUpdate(DocumentEvent e) {
disableIfEmpty(e);
}
public void insertUpdate(DocumentEvent e) {
disableIfEmpty(e);
}
public void removeUpdate(DocumentEvent e) {
disableIfEmpty(e);
}
public void disableIfEmpty(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
}
}
Related examples in the same category