Lock all buttons in a panel

S

Si

Is there any way to "disable" a panel, or lock all buttons in a panel
without any long drawn out code?
 
K

Karl von Laudermann

Si said:
Is there any way to "disable" a panel, or lock all buttons in a panel
without any long drawn out code?

I suppose you could call getComponents() on the panel, and then loop
through the returned array to disable them all. Of course, if you have
sub-containers with other controls, then you'll have to do this
recursively.

AFAIK, there's no method to simply disable an entire panel of
controls.
 
A

Andrew Thompson

Is there any way to "disable" a panel,
or lock all buttons in a panel
without any long drawn out code?

<untested>
Panel.setEnabled(false);
</untested>

And in future, please do not cross-post
to three groups. F'ups set to c.l.j.g.
 
F

Filip Larsen

Is there any way to "disable" a panel, or lock all buttons in a panel
without any long drawn out code?

How about something like this general code:

import java.awt.*;

public static void setChildrenEnabled(Container container, boolean
enabled) {
Component[] children = container.getComponents();
for (int i = 0; i < children.length; i++) {
children.setEnabled(enabled);
if (children instanceof Container) {
setChildrenEnabled(children,enabled);
}
}
}

Add appropriate tests if you only want certain components to be
disabled. If you use custom derived panels you can extend the setEnabled
method to call setChildrenEnabled directly instead of the recursive call
used above.

It is a bit drawn out, but you only have to write it once.


Regards,
 
S

Stephan Wehner

Is this "long drawn out code"??


/**
* On all subcomponents contained in component which are instances
of the classFilter class
* setEnabled(enabled) is called.
* Only subComoponents extending JComponent are "traversed"
* @param component topComponent to traverse
* @param classFilter components of which class to set enabled
* @param enabled
*/

static void setEnableSubComponent(JComponent component, Class
classFilter, boolean enabled)
{
if (classFilter.isAssignableFrom(component.getClass()))
{
component.setEnabled(enabled);
return; // assuming there will not be interesting subcomponents
}
for (int i =0; i<component.getComponentCount();i++)
{
Component subComponent = component.getComponent(i);
if (subComponent instanceof JComponent)
{
setEnableSubComponent((JComponent) subComponent, classFilter,
enabled);
}

}
}

Invoke with

setEnableSubComponent(yourPanel, JButton.class, false);

The class filter might be replaced with other filters.

Stephan

__________________________
Stephan Wehner
Editor, Traffic Life: Passionate Tales and Exit Strategies
www.trafficlife.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top