• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Disabling a JPanel and all it's components

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am having a problem. Let me explain by simplifying the GUI to a simple frame in which I have a JCheckbox and a JPanel mypanel.
mypanel has a Textfield tf,JComboBox cmb as it's components.
So now if I deselect the JCheckbox, all the components in the mypanel should be disabled. I used a code like:
[code]
mycheckbox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(mycheckbox.isSelected())
mypanel.setEnabled(true);
else
mypanel.setEnabled(false);
}
});
[code]
but the components inside the panels are not disabled. In my actual program I have a large number of different kinds of components in mypanel.So disabling/enabling each of them on each actionPerformed of the mycheckbox will be laborious. Isn't there any way by which I can disable/enable the mypanel to disable/enable all the components in it?
regards
Tanveer
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here is what to do.



------------------
Happy Coding,
Gregg Bolinger
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
thanks for the help. Gee, I forgot about that method. I should have looked at the APIs but that never crossed my mind.
thanks again.
Regards
Tanveer
 
Ranch Hand
Posts: 310
18
MS IE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgive me for excavating this 14 years old thread, but I have some useful code that can help future Googlers. This thread is in top 5 for "disable all jpanel components" phrase.

The code provided by Gregg works fine, but it will not disable components in nested containers.

Here is a recursive method that extracts all the components from given container, including components in nested containers:



Later the components can be disabled this way:

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can check out Disabled Panel.

The approach is similar to the above solution excepts that it only disables components that are currently enabled and keeps track of these components in a List. When you enable the components, only components found in the List will be enabled.

It also provides a second approach where a GlassPane is used. In this case the actual components are not disabled but the glass pane intercepts all keyboard and mouse events and paints the panel as semi transparent to is acts disabled.
 
reply
    Bookmark Topic Watch Topic
  • New Topic