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 つの長い文字列"[3, 2, 1]" として追加されます。なぜこれが起こるのか誰か教えてもらえますか?

前もって感謝します、

ローダルヌー

PSこれが私の質問の正しい方法であることを願っています。私の質問が誰もが理解できるほど明確であることを願っています.

4

1 に答える 1

2

一見すると、問題は次の2つのうちの1つである可能性があります。

  1. 値"[321]"がloadComboメソッドから返されます。より具体的にはcf.getProperty()。提供された文脈からは明らかではありませんcf
  2. 値"[32 1]"は、コード内の他のポイントでコンボボックスに追加されました。その場合configName.removeAllItems()は、コレクション内の各アイテムを削除してから追加するのではなく、を使用してみてください。

さらに、loadComboこのメソッドでは、config.propertiesファイルをPropertiesオブジェクトにロードしてから、何もしません。あなたの意図は、cfオブジェクトではなく、そのファイルから構成プロパティをロードすることだったようです。

于 2012-11-01T15:24:40.840 に答える