Article View: borland.public.jbuilder.java.swing
Article #12034Re: using setEnabled(b) on a JPanel
From: Emil Lefkof
Date: Thu, 25 Feb 1999 00:00
Date: Thu, 25 Feb 1999 00:00
121 lines
4583 bytes
4583 bytes
Tim,
I had the same problem. What we came up with is looping through all of a
containers components and setting them disabled. The only one we don't
disable are labels and Grids because if you disable a grid you can no longer
scroll through it. GlassPane works well, but only if you want to disable
the whole frame. We wanted to just make certain panels readonly. So I
suggest passing this procedure a panel with the items you want to disable
and it works great. It even recurses through tabbed panes if you pass the
parent of the tabbed pane to this procedure. Good luck.
Here is the code :
/**
* Recursively sets the "enabled" flag for a component (e.g., a panel).
*
* @param container the component who should be enabled/disabled
* @param enabled true or false
**/
public static void setComponentEnabled(Container container, boolean
enabled) {
int controlCount = container.getComponentCount();
Component component;
for (int loop = 0; loop < controlCount; loop++) {
component = container.getComponent(loop);
if (component instanceof JLabel) {
continue; //do nothing
} else if (component instanceof borland.jbcl.control.GridControl) {
borland.jbcl.control.GridControl gc =
(borland.jbcl.control.GridControl)component;
gc.setReadOnly(true);
continue;
}
component.setEnabled(enabled);
// Set subcomponent .
if (component instanceof JMenu) {
JMenu mnu = (JMenu)component;
for (int i = 0; i < mnu.getMenuComponentCount(); i++)
setComponentEnabled((Container)mnu.getMenuComponent(i), enabled);
}
else if (component instanceof JTabbedPane) {
JTabbedPane tp = (JTabbedPane)component;
for (int i = 0; i < tp.getTabCount(); i++) {
setComponentEnabled((Container)tp.getComponentAt(i), enabled);
}
}
else if (component instanceof JTabbedPane) {
JTabbedPane tp = (JTabbedPane)component;
for (int i = 0; i < tp.getTabCount(); i++) {
setComponentEnabled((Container)tp.getComponentAt(i), enabled);
}
}
else if (component instanceof Container)
setComponentEnabled((Container)component, enabled); // Recurse
Containers
}
}
Timothy E. Raborn wrote in message <36C0999F.210C1476@ametro.net>...
I've tried using setEnabled(false) on a JPanel hoping all of the
components within that panel would be disabled, but I guess this method
doesn't propagate down the component tree.
I then tried sub classing JPanel and overriding the setEnabled method as
follows:
public void setEnabled(boolean b)
setEnabledComponentTree(this, b);
repaint(); // not really sure if this is necessary
}
private void setEnabledComponentTree (Component c, boolean b)
Container c1 = (Container) c;
if (c1.getComponentCount() > 0)
for (int i = 0; i < c1.getComponentCount(); i++)
setEnabledComponentTree(c1.getComponent(i), b);
}
}
else
//c.setEnabled(b); <- giving me problems with stack overflows
(recursive loops)
super.setEnabled(b);
}
}
This is not having an effect on my sub components either, though it
appears as though the logic flow of the program is OK (judging by my debug
output, which I didn't show in the above code).
Does anyone see a problem with my code or is there another way of doing
this? My guess is that the problem may lie in the "super.setEnabled(b)"
call.
I should add that I "sort of" had this working at some point, and I
think it may have stopped working completely when I upgraded to Swing 1.1.
The code was developed using JBuilder and its built-in jdk (1.1.6). I say
"sort of" because it didn't really work for all components. You could still
interact with JComboBoxes for one.
Any help is much appreciated!
Thanks
-Tim
Technical Details
Message-ID:
<7b434n$edv12@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<36C0999F.210C1476@ametro.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,d047d73010301ae2 X-Google-Attributes: gid111d45,public From: "Emil Lefkof" <elefkof@fcg.com> Subject: Re: using setEnabled(b) on a JPanel Date: 1999/02/25 Message-ID: <7b434n$edv12@forums.borland.com> X-Deja-AN: 448405123 References: <36C0999F.210C1476@ametro.net> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2120.0 Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing