How to DISABLE group of Components in JPanel

  • Follow


Hi Folks,

I have JPanel component with bunch of JLabels, JButtons, JTextField etc 
in it.

I was wondering if there is any way I could disable(and enable also) all 
the components in my JPanel instead of picking every component 
individually and doing setEnabled(false) on it ?

Best regards,
VK
0
Reply Vk 3/7/2005 10:02:56 PM

In article <d0iiti$dhq$1@news1nwk.SFbay.Sun.COM>,
 Vk <trulytrojan@netscape.net> wrote:

> Hi Folks,
> 
> I have JPanel component with bunch of JLabels, JButtons, JTextField etc 
> in it.
> 
> I was wondering if there is any way I could disable(and enable also) all 
> the components in my JPanel instead of picking every component 
> individually and doing setEnabled(false) on it ?
> 
> Best regards,
> VK

If you refer to the API docs for JComponent's setEnabled method, it 
specifically states that disabling a component does not disable its 
children.  So I would suggest that your best best is to use a custom 
JPanel class, even if only to override the setEnabled method so that you 
enable and disable child components as you wish.

= Steve =
-- 
Steve W. Jackson
Montgomery, Alabama
0
Reply Steve 3/7/2005 10:13:23 PM


Steve W. Jackson wrote:
> In article <d0iiti$dhq$1@news1nwk.SFbay.Sun.COM>,
>  Vk <trulytrojan@netscape.net> wrote:
> 
> 
>>Hi Folks,
>>
>>I have JPanel component with bunch of JLabels, JButtons, JTextField etc 
>>in it.
>>
>>I was wondering if there is any way I could disable(and enable also) all 
>>the components in my JPanel instead of picking every component 
>>individually and doing setEnabled(false) on it ?
>>
>>Best regards,
>>VK
> 
> 
> If you refer to the API docs for JComponent's setEnabled method, it 
> specifically states that disabling a component does not disable its 
> children.  So I would suggest that your best best is to use a custom 
> JPanel class, even if only to override the setEnabled method so that you 
> enable and disable child components as you wish.
> 
> = Steve =

Right.

Override setEnabled() to resurce into all children and child containers 
calling setEnabled() on each one.

Rogan
-- 
Rogan Dawes

*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
0
Reply Rogan 3/8/2005 8:05:10 AM

first crate an array of type Component type for example

 final Component compo[]=  new Component[] {
        
        jTextField1, jCheckBox1 }.

then

 for (int i = 0; i < components.length; i++)
{
  if(compo[i] instanceof JCheckBox)
 (JCheckBox)(  compo[i])).setEnabled(false);
  else if ( compo[i] instanceof JTextField)
 (JTextField)(  compo[i])).setEnabled(false);
}

This is one alternative there may be other efficient alternative

for example find if there is a way for getting all the components of a
panel in an array of strings and using the same for loop as above check 
the type of components using "instance of" and apply setEnabled(false);

I hope there must be a way to get the list of all the components of a
container

praveen

-- 
Message posted via http://www.javakb.com
0
Reply Praveen 3/8/2005 8:55:04 AM

Praveen homkar via JavaKB.com wrote:
> first crate an array of type Component type for example
> 
>  final Component compo[]=  new Component[] {
>         
>         jTextField1, jCheckBox1 }.
> 
> then
> 
>  for (int i = 0; i < components.length; i++)
> {
>   if(compo[i] instanceof JCheckBox)
>  (JCheckBox)(  compo[i])).setEnabled(false);
>   else if ( compo[i] instanceof JTextField)
>  (JTextField)(  compo[i])).setEnabled(false);
> }
> 
> This is one alternative there may be other efficient alternative
> 
> for example find if there is a way for getting all the components of a
> panel in an array of strings and using the same for loop as above check 
> the type of components using "instance of" and apply setEnabled(false);
> 
> I hope there must be a way to get the list of all the components of a
> container
> 
> praveen
> 

Too complicated ;-)

overriding setEnabled in JPanel:

public void setEnabled(boolean enabled) {
     super.setEnabled(enabled);
     setChildrenEnabled(this, enabled);
}

private void setChildrenEnabled(Container container, boolean enabled) {
     for (int i=0; i<container.getComponentCount(); i++) {
         Component comp = container.getComponent(i);
         comp.setEnabled(enabled);
         if (comp instanceof Container)
             setChildrenEnabled((Container) comp, enabled);
     }
}

Rogan
-- 
Rogan Dawes

*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
0
Reply Rogan 3/8/2005 10:16:41 AM

4 Replies
504 Views

(page loaded in 0.071 seconds)

Similiar Articles:













7/24/2012 11:25:43 AM


Reply: