可能的重复:
如何从文本文件填充 JComboBox? https://stackoverflow.com/questions/3173149/how-do-i-populate-jcombobox-from-a-text-file
我是 Java 编程新手,只有 2 个月的经验。谁能帮我填充一个JComboBox
一个由 5 行组成的文本文件?我在 Google 上查看了代码,但不断出现错误。
private void populate() {
String[] lines;
lines = readFile();
jComboBox1.removeAllItems();
for (String str : lines) {
jComboBox1.addItem(str);
}
}
这是readFile()。从这个网站 http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml
private String[] readFile() {
ArrayList<String> arr = new ArrayList<>();
try {
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
arr.add(strLine);
}
in.close();
} catch (Exception e) {
}
return arr.toArray(new String[arr.size()]);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)