Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all components in a JPanel

In my JPanel I have many components, including other JPanels, JLabels, JTextAreas, and JButtons. Becuase I want to implement a tutorial mode where another window appears and everything in my main JPanel is disabled as the new window explains each 'feature' one by one... I want a to know how to disable all the components that are inside my origiinal JPanel. I know you can use:

component.setEnabled(false);

But I don't want to write it for each component in my JPanel. I would like to know if it's possible to disable ALL components within my JPanel with a for loop or something?

Note: There are also component in nested JPanels, like the order would be

Main JPanel ---> Nested JPanel ---> Component

I also want the Final components to also be disabled...

Thanks! All help is appreciated!

like image 966
XQEWR Avatar asked Oct 11 '13 18:10

XQEWR


2 Answers

I used the following function:

void setPanelEnabled(JPanel panel, Boolean isEnabled) {
    panel.setEnabled(isEnabled);

    Component[] components = panel.getComponents();

    for (Component component : components) {
        if (component instanceof JPanel) {
            setPanelEnabled((JPanel) component, isEnabled);
        }
        component.setEnabled(isEnabled);
    }
}
like image 194
Kesavamoorthi Avatar answered Oct 11 '22 19:10

Kesavamoorthi


Check out Disabled Panel for a couple of solutions.

One uses a disabled GlassPane type of approach and the other recursively disables components while keep track of the components current state so it can be enable properly later.

like image 8
camickr Avatar answered Oct 11 '22 20:10

camickr