我正在製作一個應用程序,現在我需要用一個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;
}
以下是結果的截圖:
正如你可以看到所有的值被添加作爲1長字符串「[3,2,1]」在組合框。誰能告訴我爲什麼會發生這種情況?
由於提前,
Lordarnoud
附:我希望這是提問我的問題的正確方法,我希望我的問題對每個人都能夠理解。
爲什麼你刪除然後添加一個項目?還要在返回部分調試你的loadCombo方法,看看列表中是否有「[3 2 1]」。如果它不在那裏。這是你的方法在添加和刪除組合框中的項目的工件 –
請添加一個標籤,以澄清你的意思是哪個UI框架(看起來像Swing?) – kleopatra
@Masood_Mj:我這樣做是因爲否則組合框只會添加值而不是替換舊值,這會導致值在comboBox中出現兩次。 –