disabling children of a JPanel
Suppose I have a hierarchy like this:
JPanel panel1;
JCheckBox cb1;
JCheckBox cb2;
JRadioButton rb1;
JRadioButton rb2;
...
I have a condition where I want to set individual groups of controls within the panel to be enabled/disabled. That works fine. (e.g. enable cb1 and cb2 when one condition is true, disable them when it is false.)
I would like to disable and re-enable the whole panel. If I call panel1.setEnabled(false)
this does not work, it only disables the panel, but does not affect its children.
If I enumerate the panel's children, and call setEnabled(false)
on e开发者_如何学Cach of them, that would work, but then I would have to store the child enabled state when I re-enable the panel.
Is there a simpler way?
If I enumerate the panel's children, and call setEnabled(false) on each of them, that would work, but then I would have to store the child enabled state when I re-enable the panel.
The Disabled Panel entry has a solution for this approach as well as a "container level" glass pane approach.
You can put a glass pane over the panel to have it intercept the events.
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
JXLayer is a great way to achieve it. It can be used for any Swing component.
//una forma de recorrer todos los elementos dentro de un jpanel
Component[] components = jPanelX.getComponents();
for (int i = 0; i < components.length; i++) {
if(components[i].getClass().getName().toString().equals("javax.swing.JTextField")){
components[i].setEnabled(false);
}
}
精彩评论