Inner Class Listeners

OK: Implement the interface yourself.

public class MyDialog extends JDialog implements ActionListener {

    public MyDialog() {
        JButton okButton = new JButton("OK");

        okButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event) {
        setVisible(false);
    }
}

Better: Have an inner class implement the interface.

public class MyDialog extends JDialog {

    public MyDialog() {
        JButton okButton = new JButton("OK");

        okButton.addActionListener(new OKButtonListener());
    }

    class OKButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            setVisible(false);
        }
    }
}

Best: Have an anonymous inner class implement the interface.

public class MyDialog extends JDialog {

    public MyDialog() {
        JButton okButton = new JButton("OK");

        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                setVisible(false);
            }
        });
    }
}

*