如何从文本文件中填充JComboBox? [英] How do I populate JComboBox from a text file?
本文介绍了如何从文本文件中填充JComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从文本文件中填充 JComboBox
?
How do I populate a JComboBox
from a text file?
推荐答案
非常含糊的问题。你是说你想要每行一个条目吗?如果是这样,你想使用类似BufferedReader的东西,读取所有行,将它们保存为String数组。创建一个传入该String构造函数的新JComboBox。
Very vague question. Are you saying you want one entry per line? If so you want to use something like a BufferedReader, read all the lines, save them as a String array. Create a new JComboBox passing in that String constructor.
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);
这篇关于如何从文本文件中填充JComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文