0

I have a JavaFX app using JRE1.8.0 u40. I converted my Swing JFileChooser Open and Save to the newer JavaFX FileChooser Open and Save, Windows7 style Dialog box. But I have not found an equivalent JavaFX FileChooser method to replace the JFileChooser method I'm using for deleting a file(s) as shown below:

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());
    }

Is there a similar solution to the above for JavaFX using FileChooser or do I use the showOpenDialog(null) with setTitle("Delete Pixel Art File")?

sgroen
  • 29
  • 1
  • 8

2 Answers2

1

You can easily perform that task using javafx like below :

@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");
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
  • Looks good, however printing `"Error Selection"` if `fileChooser.showOpenMultipleDialog(null);` returns `null` is not a good idea. `null` is returned, if the dialog is canceled. Since canceling the dialog is an option for the user and not an unexpected behaviour, it should definetly not be handled by printing a error message somewhere. – fabian Nov 17 '15 at 12:34
0

The above code was very helpful and works best with the other suggestion of checking for null and adding throws IOException to the deleteFile() method.

sgroen
  • 29
  • 1
  • 8