// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SuperEnableDisable extends JFrame implements ActionListener {
    private JPanel real;
    public SuperEnableDisable() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Demo");
        getContentPane().setLayout(new BorderLayout());
        JButton toggle = new JButton("Toogle");
        toggle.addActionListener(this);
        getContentPane().add(toggle, BorderLayout.SOUTH);
        real = new JPanel();
        real.setLayout(new GridLayout(1, 2));
        JPanel real1 = new JPanel();
        real1.setLayout(new GridLayout(2, 1));
        real1.add(new JButton("A"));
        real1.add(new JButton("B"));
        real.add(real1);
        JPanel real2 = new JPanel();
        real2.setLayout(new GridLayout(2, 1));
        real2.add(new JButton("C"));
        real2.add(new JButton("D"));
        real.add(real2);
        getContentPane().add(real, BorderLayout.CENTER);
        pack();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        setRecursiveEnabled(real, !real.isEnabled());
    }
    private void setRecursiveEnabled(Component c, boolean status) {
        c.setEnabled(status);
        if(c instanceof Container) {
            Container c2 = (Container)c;
            for(Component sc2 : c2.getComponents()) {
                setRecursiveEnabled(sc2, status);
            }
        }
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new SuperEnableDisable();
                f.setVisible(true);
            }
        });
    }
}
