Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

public class Main {

    public Main() {
        JButton button = new JButton("foo");
        JTextField textField = new JTextField(10);
        Document document = textField.getDocument();
        document.addDocumentListener(new JButtonStateController(button));

        JOptionPane.showMessageDialog(null, textField);

    }

}

class JButtonStateController implements DocumentListener {
    JButton button;

    JButtonStateController(JButton button) {
        this.button = button;
    }

    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);
    }
}