伙计们,我不知道如何在列表视图的某个目录中显示所有文件。我以为使用这里其他帖子上显示的arraylist,但是它没有帮助,因为我有一个按钮来查找和选择目录,然后设置一个listview,以便向我们显示该目录内的所有文件,这是我的代码,可能很愚蠢问题可能不是,但我需要非常迅速的帮助
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private TextField textfield;
@FXML
private AnchorPane anchorid;
@FXML
private ListView listOfFiles;
@FXML
private String handleButtonAction(ActionEvent event) {
final DirectoryChooser dirchooser = new DirectoryChooser();
Stage stage = (Stage) anchorid.getScene().getWindow();
File file = dirchooser.showDialog(null);
String s = "";
if (file != null) {
System.out.println("Path: " + file.getAbsolutePath());
s = ""+file.getAbsolutePath();
textfield.setText(file.getAbsolutePath());
}
return s;
}
@FXML
private void teste(ActionEvent event, String s) {
File folder = new File(s);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
//Empty
}
}
编辑:顺便说一句,我忘了说字符串s只是返回了在另一个方法中获取到列表视图之后的内容