Java Swing - How to disable a JPanel?
Use the JXLayer, with LockableUI.
The Disabled Panel provides support for two approaches. One to recursively disable components, the other to "paint" the panel with a disabled look.
The following method uses recursion to achieve this. Pass in any Container
, and this method will return a Component
array of all of the non-Container components located anywhere "inside" of the Container
.
private Component[] getComponents(Component container) {
ArrayList<Component> list = null;
try {
list = new ArrayList<Component>(Arrays.asList(
((Container) container).getComponents()));
for (int index = 0; index < list.size(); index++) {
for (Component currentComponent : getComponents(list.get(index))) {
list.add(currentComponent);
}
}
} catch (ClassCastException e) {
list = new ArrayList<Component>();
}
return list.toArray(new Component[list.size()]);
}
}
Simply loop through the elements of the returned array and disable the components.
for(Component component : getComponents(container)) {
component.setEnabled(false);
}
The panel should have a getComponents()
method which can use in a loop to disable the sub-components without explicitly naming them.