Disable all components AND subcomponents

I have a bunch of JButtons, JText, JTree, etc. in a JPanel and I want to disable/grey-out all of them. But some of the JComponents are in a Box. They don't get disabled. The code I am using now:
     public void setEditable(boolean bool) {
          for (Component component: getComponents()) {
               component.setEnabled(bool);
So the above works for all the components that are not in the Box
I ended up doing the below (checks if the component is a Box and if it is, iterate through that enabling/disableing JComponents):
     public void setEditable(boolean bool) {
          for (Component component: getComponents()) {
               component.setEnabled(bool);
               if (component.getClass().toString().equals("class javax.swing.Box")) {
                    for(Component subComponent: ((Box)component).getComponents()) {
                         subComponent.setEnabled(bool);
So big question is, is there some better way of doing this, or someway which is more generic?

Well you could extend Box and override the setEnabled method to call setEnabled of all it's components, and then use that as your Box instead.
But I have a couple of suggestions regarding the code you posted.
1. You should use instanceof operator to check whether the component is an instance of Box
2. You don't handle the case when there's a Box within a Box.
e.g.,
public void setEditable(boolean editable) {
    for (Component c : getComponents()) {
        setEditable(c, editable);
private void setEditable(Component c, boolean editable) {
    c.setEnabled(editable);
    if (c instanceof javax.swing.Box) {
        for (Component subC : ((javax.swing.Box)c).getComponents())
            setEditable(subC, editable);
}

Similar Messages

  • Disable All Components in GUI?

    Hi, I am writing a gui using java.awt/javax.swing
    I want to disable all components in my gui (ie. setting all components of my gui to the disabled state by using setEnabled(false) from the java.awt.Component class) except for the Exit Button of the title bar (so that one can still exit the program via the exit button).
    I try using JFrame.setEnabled(false); however, the close button is also disabled. I can go about disabling the panes, the menus, and the buttons separately but that would be too troublesome.
    Anyone have some pointers? Thanks!

    hi
    I think this block of code solve your problem.
              JFrame f=new JFrame();
              f.setSize(400,400);
              f.getContentPane().setLayout(new FlowLayout());
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JButton b1=new JButton("Demo1");
              JButton b2=new JButton("Demo2");
              f.getContentPane().add(b1);
              f.getContentPane().add(b2);
              Component[] c=f.getContentPane().getComponents();
              System.out.println(c.length);
              for(int i=0;i<c.length;i++)
                   c.setEnabled(false);
              b1.setEnabled(true);
              f.setVisible(true);

  • Firefox 8 and 9beta. Back and Forward buttons greyed out. Right click menu also has forward and back greyed out. Updated all plugins, then disabled all plugins and extensions and started in safe mode. No change.

    My forward and back buttons are greyed out after update to Firefox 8. Tried to move to 9 beta, but same behavior. Back and Forward also greyed out in right click menu.
    Attempted to fix by updating all plugins and extensions, then disabled all plugins and extensions, and then tried to start in safe mode. Nothing worked.

    You could have backed up your profile folder. If you did, [[Recovering important data from an old profile]] can help :)

  • Firefox frequently 'not responding' having auto-installed version 10 yesterday. No previous problems. Have disabled all plugins and uninstalled and then re-installed Firefox several times today. Problem persists. Can anyone help please?

    Firefox frequently 'not responding' having auto-installed version 10 yesterday. No previous problems. Have disabled all plugins and uninstalled and then re-installed Firefox several times today. Problem persists making continued use of this browser very unlikely unless a quick solution is found! Can anyone help please?

    Perform the suggestions mentioned in the following articles:
    * [[Firefox is already running but is not responding]]
    * [[Firefox hangs]]
    Check and tell if its working.

  • After the last update on iTunes I have not been able to get into the program. I have uninstalled all components and reinstalled and now it is telling me I am missing .dll files. I am so frustrated. I probably lost my entire library except what I purchased

    After the lastest update on iTunes I have not been able to get into the program. I have uninstalled all components and reinstalled and now it is telling me I am missing .dll files. I am so frustrated. I probably lost my entire library except what I purchased

    Your library should be safe.
    See Troubleshooting issues with iTunes for Windows updates.
    tt2

  • Can't get rid of ads by Browse to Save and I've disabled all plugins and add-ons

    In Firefox, on my FaceBook page and yahoo mail, there are Browse to Save ads everywhere! They are driving me insane. I disabled all add-onos and plugins and the problem remains. I clicked on on the ad and went to browse to save's site where they say they tell you how to uninstall it, but there is no way to uninstall it because it doesn't show up in plugins or add-ons. Help!

    'browse to save' goes by a few names apparently-
    mine called itself Vaudix (uninstalled it and problem is gone)
    other names include Yontoo apparently
    hope that helps- my first and only ? post!

  • Fire Fox V.3.16- Yahoo Mail, Cannot open my mail to read unless I open in New Tab. Cannot create mail. Cleared Cache and History. Disabled all Extensions and non relevant Add-ons. Was working last week??

    Also IE8 has same problem.. I only use FireFox but tried IE after I couldn't get any where with Fire Fox??
    Also have a problem with e bay, cannot bid on items, nothing happens. Using XP with all updates and service packs installed.

    Also IE8 has same problem.. I only use FireFox but tried IE after I couldn't get any where with Fire Fox??
    Also have a problem with e bay, cannot bid on items, nothing happens. Using XP with all updates and service packs installed.

  • Firefox won't start even after disabling all plugins and reinstalling

    Two days ago I updated my graphics card drivers (nvidia geforce) and it had some problems. I went into live chat support and after reinstalling some files it worked. Right after that Firefox wouldn't open, prompting me to restart or close. Hitting restart would take me the same place. It only opened in safe mode, where I deactivated the plugins related to the graphics card, and Firefox started normally.
    Yesterday it didn't start again, even with those plugins deactivated. so I started in safe mode and deactivated all other plugins and it started. Today happened the same again but now I didn't have anything to deactivate so I could never start it. I unistalled and downloaded the installation file again and installed. The problem remained.
    I went to the page Firefox crashes - Troubleshooting and filled in the crash report to mozilla crash reports page with the ID: 6cf4a525-9152-46f9-96f2-7ef282131023
    The problem looks like it has something to do with the file mswsock.dll, but I'm affraid of deleting it.
    Please help me.
    Thank you,
    Pedro

    hello pedro, this crash is caused by the "Media Accelerator" part your nvidia graphics driver. please see if you can selectively disable it in the nvidia control panel or even uninstall it. otherwise please try [[Upgrade your graphics drivers to use hardware acceleration and WebGL|updating your graphics driver]], or in case this doesn't solve the issue or there is no new version available at the moment, disable hardware acceleration in ''firefox > options > advanced > general''.

  • How can I delete all components of Firefox 8 for Mac. Again...all components and hidden files. (YES, I did check the Moz web site but the page was essentially blank

    I have an unstable install of Mozilla firefox 8 and need to delete ALL files from my mac running OSX 10.7.2. I went to Mozilla web site and got this but it does not populate with content:
    http://support.mozilla.com/en-US/kb/Uninstalling%20Firefox?s=uninstall+firefox+mac&r=0&as=s#w_removing-user-data-and-settings
    But nothing appears at this URL.

    Please note I need all files removed including the hidden ones.
    thx

  • Besides being unable to view some images on some pages, some windows open too wide inside the current window. If I start up in safe mode, everything works OK. But if I disable all extensions and add-ons, I still have the problem.

    Too long

    This had no affect on my problem. Sorry, but no help at all. I can enter safe mode with nothing checked in the dialog and the problems go away. Any further ideas?

  • Ssis package error need help to fix it. though disabled all triggers and droped all the foreign keys and truncated tables on pipeline constraints.

    [PartyName1 [6672]] Error: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
    An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 11.0"  Hresult: 0x80004005  Description: "Transaction (Process ID 1414) was deadlocked on lock resources with another process and has been chosen as the
    deadlock victim. Rerun the transaction.".

    you need to provide Details about your package like what is source/destination.. any SQL query/where is the exact deadlock point.
    Please check the following links which may help you.
    http://blogs.technet.com/b/fort_sql/archive/2011/12/12/get-rid-of-deadlocks.aspx
    http://msdn.microsoft.com/en-us/library/ms191242.aspx
    Let us TRY this |
    My Blog :: http://quest4gen.blogspot.com/

  • I can't print from Firefox (on a MacBook) since I tried to instal the new version. How do I uninstall it (all components), and then reinstall?

    Can't print from Firefox - it crashes.
    == This happened ==
    Every time Firefox opened
    == I upgraded ==
    == User Agent ==
    Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7

    Also can't print from FF 3.6.12, but it doesn't crash. There's no error message, no job gets queued to the printer. Other applications (including Safari) print without a problem.

  • As i'm installing itunes, I have this problem come up I have tried installing reinstalling all components and still doesn't work this is the error message?

    "could not write value quicktime task to key \SOFTWARE\Microsoft\Windows\CurrentVersion\Run verify that you have sufficient access to that key, or contact your support personnel.
    if anyone can help that would be great thanks!

    I have tried that but still doesn't work, I have right clicked the itunes setup right as administrator, still has the key problem, this time, it is the ituneshelper key same location, but ituneshelper key

  • The back button no longer works. I have disabled all add-ons, rebooted & reset the defaults for the browser and nothing has changed. This happens on all webpages.How do I get the back button to light up again?

    the back button no longer works. I have disabled all add-ons,rebooted & reset the defaults for the browser and nothing has changed. This happens on all webpages.How do I get the back button to light up again?

    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions or if hardware acceleration is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    *Don't make any changes on the Safe mode start window.
    *https://support.mozilla.com/kb/Safe+Mode
    If it does work in Safe-mode then disable all extensions and then try to find which is causing it by enabling one at a time until the problem reappears.
    *Use "Disable all add-ons" on the [[Safe mode]] start window to disable all extensions.
    * Close and restart Firefox after each change via "File > Exit" (Mac: "Firefox > Quit"; Linux: "File > Quit")
    Another possible cause is a problem with the file places.sqlite that stores the bookmarks and the history.
    *http://kb.mozillazine.org/Bookmarks_history_and_toolbar_buttons_not_working_-_Firefox
    *https://support.mozilla.com/kb/Bookmarks+not+saved#w_places-database-file

  • "Settings unavailable" in iOS app. And cannot uncheck "Disable all notifications" on web.

    I guess this problem started a few months back, when there was two "disable all notifications" checkbox in the settings. I happened to check both of them, and later, Spotify found out that they have put 2 boxes of the same function and removed one of them. I'm guessing that the issue started at that exact moment. The box was removed from both iOS and web. My setting got stuck "checked" and cannot be unchecked. Just guessing. What else could be causing this? Why everytime I uncheck "Disable all notifications" and save it, it gets checked again and it is impossible to uncheck with any browser on any platform, why? Why I see that "Setting Unavailable" page on my iPhone? I made sure Spotify app's notifications are on in iOS's settings. I believe, only someone from the team will be able to solve this by tweaking my account's setting file on the server side. I see no other solutions.

    I am having the same problem on iPhone 5 and what's more on desktop Mac OS application I am not receiving any new album notifications for about 3 months now. This is really annoying for I'm following about 360 artists and there's no simpler way of getting to know new album releases.

Maybe you are looking for