编程语言
首页 > 编程语言> > java – 如何从文本文件中填充JComboBox?

java – 如何从文本文件中填充JComboBox?

作者:互联网

如何从文本文件中填充JComboBox?

解决方法:

很模糊的问题.你是说你想要每行一个条目?如果是这样,你想使用类似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);

标签:populate,jcombobox,java,text-files
来源: https://codeday.me/bug/20190925/1816656.html