2012-11-01 60 views
1

我正在製作一個應用程序,現在我需要用一個arrayList類型中的其他類來獲取所有值的組合框。ComboBox將所有值一起添加爲一個值

這是我用它來填補我的組合框代碼:

public void setComboBox(){ 
    MessageConsole mc = new MessageConsole(textArea); 
    //The redirectOut will redirect the text from the System.out.prtnln to the text area. 
    mc.redirectOut(); 
    List<String> arrayList = new ArrayList<String>(); 
    if(gf.loadCombo("config") != null){ 
    arrayList.addAll(gf.loadCombo("config")); 
     for (int i = 0; i < arrayList.size(); i++) { 
      String s = arrayList.get(i); 
      configName.removeItem(s); 
      configName.addItem(s); 
     } 
    } 
} 

這是從其他類代碼:

public Collection<String> loadCombo(String property) { 
    //Check if the property file is present in the given directory. 
    if(cf.checkProperty()==true){ 
     //Check is there are any properties saved in the property file. 
     if(cf.getProperty() != null){ 
      Properties prop = new Properties(); 
      FileInputStream fis; 
      try { 
       fis = new FileInputStream("config.properties"); 
       prop.load(fis); 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      //Check if the properties match with the entered configuration. 
      List<String> arrayList = new ArrayList<String>(); 
      arrayList.addAll(cf.getProperty()); 
      Collection<String> propertyList = new ArrayList<String>(); 
      for (int i = 0; i < arrayList.size(); i++) { 
       String s = arrayList.get(i); 
       if(s.startsWith(property)){ 
        System.out.println("This value has been added to the arrayList from the other class: "+s); 
        propertyList.add(cf.getPropertyValue(s)); 
       } 
      } 
      return propertyList; 
     } 

     //The system prints a message of the missing properties. 
     else{ 
      System.out.println("You need to save a configuration first."); 
     } 
    } 

    //The system prints a message of the missing property file. 
    else{ 
     System.out.println("The property file is either missing or could not be found. in de Load class"); 
    } 
    return null; 
} 

以下是結果的截圖:

enter image description here

正如你可以看到所有的值被添加作爲1長字符串「[3,2,1]」在組合框。誰能告訴我爲什麼會發生這種情況?

由於提前,

Lordarnoud

附:我希望這是提問我的問題的正確方法,我希望我的問題對每個人都能夠理解。

+0

爲什麼你刪除然後添加一個項目?還要在返回部分調試你的loadCombo方法,看看列表中是否有「[3 2 1]」。如果它不在那裏。這是你的方法在添加和刪除組合框中的項目的工件 –

+0

請添加一個標籤,以澄清你的意思是哪個UI框架(看起來像Swing?) – kleopatra

+0

@Masood_Mj:我這樣做是因爲否則組合框只會添加值而不是替換舊值,這會導致值在comboBox中出現兩次。 –

回答

2

在第一外表看來問題可能是兩件事情之一:

  1. 值「[3 2 1]」從loadCombo方法返回。更具體地來自cf.getProperty()。從上下文中我們不清楚cf是什麼。
  2. 值「」[3 2 1]「已添加到您代碼中某個其他位置的組合框中。如果這可能是這種情況,請嘗試使用configName.removeAllItems(),而不是刪除然後添加集合中的每個項目。

此外,在您的loadCombo方法中,將config.properties文件加載到Properties對象中,然後不執行任何操作。看來你的意圖是從該文件加載配置屬性,而不是cf對象。

+0

感謝大家的回覆。 removeAllItems()做了訣竅!感謝您的明確評論@DixonJ。我不能相信我在API中錯過了這個方法......而且你對道具也是正確的。我有很多使用道具的方法,我甚至沒有注意到這個方法有一個,但沒有使用它。非常感謝! –

+0

不客氣,我很高興能夠提供幫助。 –