我正在制作一个应用程序,现在我需要用一个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;
}
以下是结果的屏幕截图:
如您所见,所有值都在comboBox中添加为1个长字符串“[3,2,1]”。谁能告诉我为什么会这样呢?
提前致谢,
Lordarnoud
P.S。我希望这是提出问题的正确方法,我希望我的问题足够清楚,让每个人都能理解。
答案 0 :(得分:2)
初看起来,问题可能是两件事之一:
loadCombo
方法返回值“[3 2 1]”。更具体地说,来自cf.getProperty()
。从上下文中提供的cf
是什么不清楚。configName.removeAllItems()
,而不是删除然后添加集合中的每个项目。此外,在loadCombo
方法中,您将config.properties文件加载到Properties对象中,然后对其执行任何操作。您的意图似乎是从该文件而不是cf
对象加载配置属性。