2012-02-27 31 views
1

我有具有突片的多個tabs.One一個JDialog填充複選框的動態列表,並將其添加到JPanel。這面板添加到該JTabbedPane添加到面板禁用複選框沒有被禁用

在這個動態列表中,我想根據某些條件禁用一些複選框。

問題是,即使我添加了禁用狀態的複選框,它仍然處於啓用狀態。

我不明白爲什麼它表現這樣或我哪裏去錯了?

來實現這一目標是如下的代碼片段:

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); 


    } 
} 
+1

爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-02-27 06:25:17

+0

你確定沒有別的改變複選框的狀態嗎? – 2012-02-27 06:53:25

+0

Yup.Checked它。 – shabeena 2012-02-27 06:58:50

回答

3

似乎在這個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); 
      } 
     }); 
    } 
} 

它的工作你的機器上如預期?

+0

是的,這個程序在我的機器上工作。但神祕的是,如果我也禁用了複選框,然後添加它(在我的源代碼中),甚至是其他方式,爲什麼它不工作? – shabeena 2012-02-27 06:51:39

+1

好的,我得到一個SSCCE併發布它。 – shabeena 2012-02-27 07:00:54