如何從文本文件填充JComboBox
?如何從文本文件填充JComboBox?
1
A
回答
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
1
打破您的要求爲獨立的步驟,該代碼將遵循:
1)讀取一行數據從文件 2)使用JComboBox中的addItem(...)方法將數據添加到組合框
相關問題
- 1. 如何用ArrayList填充JComboBox?
- 2. 從文件填充文本框
- 3. 從文本文件填充字典
- 4. 從文本文件填充MySQL表格
- 5. 從XML文件填充文本框
- 6. 從文本文件填充選擇框
- 7. 從Enum填充Swing JComboBox
- 8. 如何填充文本字段從一個JComboBox,在Java GUI選擇
- 9. 如何從文本文件中的單詞填充ArrayList?
- 10. 如何填充從文本文件中矢量有兩列C++
- 11. 如何使用文本文件從comobobox自動填充文本框
- 12. 如何從繪製的文本中設置文本的填充?
- 13. 從.odt文件填充QTextDocument
- 14. 從XML文件填充ListView
- 15. 從xml文件填充ViewList
- 16. 用java填充文本文件表
- 17. 從DropDownList填充文本框選擇用CascadingDropDown填充本身
- 18. 使用特定目錄的文件名填充JComboBox
- 19. 填充UILabel文本填充其寬度
- 20. 從JComboBox中檢索文本
- 21. 如何使用填充文本輸入填充其容器
- 22. 如何用文本文件項填充組合框!
- 23. vt.net,如何用文本文件填充組合框?
- 24. 如何填充HTML文件輸入的文本框?
- 25. 如何用文本文件中的內容填充Qt ListView?
- 26. 複製文本文件並用輸出文本填充副本
- 27. 填充值文件
- 28. 從url讀取文本文件並在Gridview中填充Asp.net
- 29. ASP.NET - Visual Studio:從資源文件中填充一段文本
- 30. 從文本文件填充多維數組