通过 FileChooser 删除文件?
Deleting a file(s) through FileChooser?
我有一个使用 JRE1.8.0 u40 的 JavaFX 应用程序。我将我的 Swing JFileChooser 打开和保存转换为更新的 JavaFX FileChooser 打开和保存,Windows7 样式对话框。但是我还没有找到等效的 JavaFX FileChooser 方法来替换我用于删除文件的 JFileChooser 方法,如下所示:
public static void deleteFile() throws IOException {
JFileChooser fileDialog = new JFileChooser("C:\ProgramData\L1 Art Files\");
File[] selectedFiles;
fileDialog.setSelectedFiles(null);
// Set frame properties
fileDialog.setDialogTitle("Delete Pixel Art File(s)");
//fileDialog.setLayout(new FlowLayout());
fileDialog.setSize(400, 400);
fileDialog.setVisible(true);
fileDialog.setMultiSelectionEnabled(true); // Allow multiple selection
fileDialog.setVisible(true);
int option = fileDialog.showDialog(null, "Delete");
if (option != JFileChooser.APPROVE_OPTION)
return; //user canceled the
selectedFiles = fileDialog.getSelectedFiles();
if (selectedFiles != null) { //ask the user to replace this file
int response = JOptionPane.showConfirmDialog(null, "Are you sure want to delete this file?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response != JOptionPane.YES_OPTION) return;
}
for (File f : selectedFiles) {
Files.delete(f.toPath());
}
对于使用 FileChooser 的 JavaFX 是否有与上述类似的解决方案,或者我是否使用 showOpenDialog(null) 和 setTitle("Delete Pixel Art File") ?
您可以使用 javafx 轻松执行该任务,如下所示:
@FXML
private void onDeleteAction(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Your_title_here");
List<File> selectedFiles = fileChooser.showOpenMultipleDialog(null);
if (selectedFiles != null) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Warning !");
alert.setContentText("Are you sure you want to delete these files ?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
for (File selectedFile : selectedFiles) {
selectedFile.delete();
}
}
} else {
System.out.println("Error Selection");
}
}
上面的代码非常有用,并且与检查 null
并将 throws IOException 添加到 deleteFile()
方法的其他建议配合使用时效果最好。
我有一个使用 JRE1.8.0 u40 的 JavaFX 应用程序。我将我的 Swing JFileChooser 打开和保存转换为更新的 JavaFX FileChooser 打开和保存,Windows7 样式对话框。但是我还没有找到等效的 JavaFX FileChooser 方法来替换我用于删除文件的 JFileChooser 方法,如下所示:
public static void deleteFile() throws IOException {
JFileChooser fileDialog = new JFileChooser("C:\ProgramData\L1 Art Files\");
File[] selectedFiles;
fileDialog.setSelectedFiles(null);
// Set frame properties
fileDialog.setDialogTitle("Delete Pixel Art File(s)");
//fileDialog.setLayout(new FlowLayout());
fileDialog.setSize(400, 400);
fileDialog.setVisible(true);
fileDialog.setMultiSelectionEnabled(true); // Allow multiple selection
fileDialog.setVisible(true);
int option = fileDialog.showDialog(null, "Delete");
if (option != JFileChooser.APPROVE_OPTION)
return; //user canceled the
selectedFiles = fileDialog.getSelectedFiles();
if (selectedFiles != null) { //ask the user to replace this file
int response = JOptionPane.showConfirmDialog(null, "Are you sure want to delete this file?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response != JOptionPane.YES_OPTION) return;
}
for (File f : selectedFiles) {
Files.delete(f.toPath());
}
对于使用 FileChooser 的 JavaFX 是否有与上述类似的解决方案,或者我是否使用 showOpenDialog(null) 和 setTitle("Delete Pixel Art File") ?
您可以使用 javafx 轻松执行该任务,如下所示:
@FXML
private void onDeleteAction(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Your_title_here");
List<File> selectedFiles = fileChooser.showOpenMultipleDialog(null);
if (selectedFiles != null) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Warning !");
alert.setContentText("Are you sure you want to delete these files ?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
for (File selectedFile : selectedFiles) {
selectedFile.delete();
}
}
} else {
System.out.println("Error Selection");
}
}
上面的代码非常有用,并且与检查 null
并将 throws IOException 添加到 deleteFile()
方法的其他建议配合使用时效果最好。