1

我现在正在制作一个应用程序,现在我需要用我从 arrayList 类型的另一个类中获得的所有值填充一个组合框。

这是我用来填充组合框的代码:

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 long String"[3, 2, 1]" 添加到组合框中。谁能告诉我为什么会这样?

提前致谢,

洛达努德

PS我希望这是问我问题的正确方法,我希望我的问题足够清楚,让每个人都能理解。

4

1 回答 1

2

乍一看,问题可能是以下两种情况之一:

  1. loadCombo方法返回值“[3 2 1]”。更具体地说,来自cf.getProperty(). 从提供的上下文中不清楚cf是什么。
  2. 值“[3 2 1]”在您的代码中的其他位置添加到您的组合框中。如果可能是这种情况,请尝试使用configName.removeAllItems()而不是删除然后添加集合中的每个项目。

此外,在您的loadCombo方法中,您将 config.properties 文件加载到 Properties 对象中,然后对其不执行任何操作。看来您的意图是从该文件而不是cf对象加载配置属性。

于 2012-11-01T15:24:40.840 回答