我有一个可以保存到文本文件的ListView。文本文件已成功填充,但当我尝试从文本文件中读取以填充ListView时,不会显示任何内容。此外,为了确保文本文件不是空的,我将其内容打印到完全显示的控制台。为什么列表中没有出现字符串?感谢。
if (event.getSource() == accept0) {
stage0.close();
DirectoryChooser chooser1 = new DirectoryChooser();
chooser1.setTitle("Select Folder");
File dir1 = chooser1.showDialog(null);
String line;
if (dir1.isDirectory()) {
try {
BufferedReader br1 = new BufferedReader(new FileReader(dir1.getAbsoluteFile() + "\\ListViewItems.txt"));
if (!br1.ready()) {
br1.close();
throw new IOException();
}
while ((line = br1.readLine()) != null) {
items.getItems().add(line); //adding line from text file to ListView
System.out.println(line);
}
br1.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ListView
和ObservableList
private ObservableList<String> item = FXCollections.observableArrayList();
private ListView<String> items = new ListView<String>(item);