简体   繁体   中英

Disabled checkbox added to the panel is not disabled

I have a JDialog which has multiple tabs.One of the tabs populates a dynamic list of checkboxes and adds it to the JPanel .This panel is then added to the JTabbedPane .

In this dynamic list, I would like to disable a few checkboxes based on the some condition.

The problem is even when I add a checkbox with disabled state, it is still enabled.

I cannot figure why is it behaving this way or where am I going wrong?

The code snippet used to achieve this is as follows:

private void populateComponents() 
{
    cwwObjComponentList = cwwObjOprGeneralSetings.getComponentList();
    cwwObjComponentName = cwwObjOprGeneralSetings.getComponentName();
    cwwObjComponentWithType = cwwObjOprGeneralSetings.getComponentsWithType();

    cwwObjPnlComponents.setLayout(new GridLayout(4, 2));

    String mwwStrInstallationType = null;
    if(Configuration.getParameter(ConfigSettings.InstallationType).equalsIgnoreCase("Enterprise"))
    {
        mwwStrInstallationType = StoreSettingsFrame.cwwStrEnterpriseInstallation;
    }
    else if (Configuration.getParameter(ConfigSettings.InstallationType).equalsIgnoreCase("Server"))
    {
        mwwStrInstallationType = StoreSettingsFrame.cwwStrServerInstallation;
    }
    else 
    {
        mwwStrInstallationType = StoreSettingsFrame.cwwStrClientInstallation;
    }


    for (int i = 0; i < cwwObjComponentList.size(); i++) 
    {
        cwwObjCheckbox = new JCheckBox(cwwObjComponentList.get(i));

        String mwwStrComponentType = cwwObjComponentWithType.get(cwwObjComponentList.get(i));

        if(mwwStrComponentType.equalsIgnoreCase(mwwStrInstallationType))
        {
            cwwObjCheckbox.setEnabled(true);
        }
        else
        {
            cwwObjCheckbox.setEnabled(false);//inspite of disabling few checkboxes, all appear to be enabled
        }

        cwwObjPnlComponents.add(cwwObjCheckbox);


    }
}

Seems to work just fine in this SSCCE.

DisableMe

import java.awt.*;
import javax.swing.*;

class DisableMe {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(1,0));
                for (int ii=1; ii<7; ii++) {
                    JCheckBox cb = new JCheckBox(""+ii, ii%3==0);
                    cb.setEnabled(ii%2==0);
                    gui.add(cb);
                }
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

Does it work as expected on your machine?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM