2010-07-03 105 views

回答

4

非常模糊的問題。你是說你想每行有一個條目嗎?如果是這樣你想使用類似BufferedReader的東西,讀取所有行,將它們保存爲一個String數組。創建一個傳入該String構造函數的新JComboBox。

BufferedReader input = new BufferedReader(new FileReader(filePath)); 
List<String> strings = new ArrayList<String>(); 
try { 
    String line = null; 
    while ((line = input.readLine()) != null){ 
    strings.add(line); 
    } 
} 

catch (FileNotFoundException e) { 
    System.err.println("Error, file " + filePath + " didn't exist."); 
} 
finally { 
    input.close(); 
} 

String[] lineArray = strings.toArray(new String[]{}); 

JComboBox comboBox = new JComboBox(lineArray); 
2

這裏是一個example讀取屬性文件來獲得(在組合)鍵和值(一個文本區域)。請參閱source中的enum Rule

1

打破您的要求爲獨立的步驟,該代碼將遵循:

1)讀取一行數據從文件 2)使用JComboBox中的addItem(...)方法將數據添加到組合框