2013-09-22 30 views
-2

你好,我仍然是java新手希望學習這個不錯的功能... 你好,我有4個組合框有相同的內部和內部它是如果在jComBox1中選擇的項目,那麼它應該被禁用所有其他組合框

-Select- 
Item 1 
Item 2 
Item 3 
Item 4 

,當我選擇Item 1comboBox1, 的comboBox2,comboBox3 and comboBox4有元素只有這些

-Select- 
Item 2 
Item 3 
Item 4 

,然後當我選擇Item 3comboBox2,將comboBox3 and comboBox4有這些剩餘的元素

-Select- 
Item 2 
Item 4 

人有知道如何做到這一點關於Java?我使用的GUI Builder中的NetBeans ...

編輯1

這是我的代碼

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { 
    jComboBox2.removeItem(jComboBox1.getSelectedItem()); 
    jComboBox3.removeItem(jComboBox1.getSelectedItem()); 
    jComboBox4.removeItem(jComboBox1.getSelectedItem()); 
} 

,我以後添加相同的代碼jComboBox2, jComboBox3 and jComboBox4 ... 當我去選擇-Select--Select-也不見了......而且

還有一個問題是當我已經選擇了所有想法並再次改變它......所有項目都沒有了, Ë選擇了..我只是想再次備份可用項...

EDIT 2

jComboBox1 
-Select- 
Item 1 
Item 2 <-- I select Item2, then the other combo box will remove Item 2** 
Item 3 
Item 4 

jComboBox2 
-Select- 
Item 1 
Item 3 <-- then I select Item 3 
Item 4 

jComboBox3 
-Select- 
Item 1 
Item 4 <-- then Item 4 

jComboBox4 
-Select- 
Item 1 

,但我改變了我的腦海裏......然後我需要回到jComboBox2選擇Item3 所以我選擇 jComboBox2並選擇-Select-,所以我可以選擇item3jComboBox4

但結果是 jComboBox4 空(無項目)

+0

看起來像一個單一的'JList',允許選擇多個項目會更容易,並且最終用戶更容易混淆。這個'不錯的功能'讀起來像'製作中對我無用的GUI'。 –

回答

0

當一些組合框的狀態發生改變,你不應該只刪除其他組合框項目,但還插入。 例如,如果物品1被選中,然後您決定選擇項目3,你必須從所有其他組合框刪除項目3並插入物品1

0

你可以使用某種類型的「代理」的模式來過濾單個組合框...

,而不是試圖添加和刪除不同的組合框的項目,你可以用一個單一的主組合框模型,它開始包含所有可用的項目。

然後,每個組合框都將擁有自己的「代理」模型(使用主模型作爲基礎),然後可以從組合框使用的列表中「過濾」項目。

通過這種方式,您只需告訴「代理」模型的哪些項目需要過濾掉,並允許底層API負責其餘部分。

0

要解決「選擇」問題,您可以驗證選定索引是否不同於0(假設「選擇」選項總是第一個)。

每當你改變組合時,你需要移除其他組合中的選定項目,並在其他組合中添加未選定的項目。

0

您可以更乾淨地創建所有使用循環的框和選項。此代碼未經測試,但應該可以工作。

//Declare and initialize the options that the comboboxes will have 
String[] options = {"-Select-", "Item 1", "Item 2", "Item 3", "Item 4"}; 
//Declare and initialize an array that will hold the currently selected options in each combobox by index 
//For example the currently selected value of comboBoxes[1] is selected[1] 
String[] selected = {"-Select-", "-Select-", "-Select-", "-Select-"}; 

//Declare and initialize an array of comboBoxes. 
//Four comboboxes will be created all containing the options array 
JComboBox[] comboBoxes = new JComboBox[4]; 
for(int i = 0; i < comboBox.length; i++) { 
    comboBoxes[i] = new JComboBox(options); 
} 

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { 
    //Loop through all of the comboboxes in comboBoxes 
    for(int i = 0; i < comboBoxes.length; i++) { 
     //Check to see if the current combobox in the array matches the source of your event 
     if(evt.getSource() == comboBoxes[i]) { 
      //Get the string value of the combobox that fired the event 
      String currentSelection = (String)comboBoxes[i].getSelectedItem(); 
      //Make sure that the value actually changed 
      if(!currentSelection.equals(selected[i]) { 
       //If the previous value of the combobox was "-Select-" don't add it to all the other comboboxes 
       if(!selected[i].equals(options[0])) { 
        //Add back the previous value to all comboboxes other than the one that fired the event 
        for(int j = 0; j < comboBoxes.length; j++) { 
         if(j != i) { 
          comboBoxes[j].addItem(selected[i]); 
         } 
        } 
       } 
       //If current value of the combobox is "-Select-" don't remove it from all other comboboxes 
       if(!currentSelection.equals(options[0]) { 
        //Remove the current value from all comboboxes other than the one that fired the event 
        for(int j = 0; j < comboBoxes.length; j++) { 
         if(j != i) { 
          comboBoxes[j].removeItem(comboBoxes[i].getSelectedItem()); 
         } 
        } 
       } 
      } 
      //Set the selected item for the combobox that fired the event to the current value 
      selected[i] = currentSelection; 
     } 
    } 
} 
相關問題