Possible Duplicate:
How do I populate JComboBox from a text file?可能重复:如何从文本文件中填充JComboBox?
I am new to programming Java with only 2 months of experience. Can anyone help me to populate a JComboBox
with a text file, consisting of 5 lines? I have looked at code on Google, but I keep getting errors.
我是Java新手,只有2个月的经验。任何人都可以帮我填充一个包含5行文本文件的JComboBox吗?我查看了Google上的代码,但我一直在收到错误。
1 个解决方案
#1
3
private void populate() {
String[] lines;
lines = readFile();
jComboBox1.removeAllItems();
for (String str : lines) {
jComboBox1.addItem(str);
}
}
Here is readFile(). From this site
这是readFile()。来自这个网站
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()]);
}
#1
3
private void populate() {
String[] lines;
lines = readFile();
jComboBox1.removeAllItems();
for (String str : lines) {
jComboBox1.addItem(str);
}
}
Here is readFile(). From this site
这是readFile()。来自这个网站
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()]);
}