Question

How does the showSaveDialog( ) method work? I know it returns the selected file but why does it not save the file?

Below is an extract of code I found online.

buttonSave.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent event) {
          FileChooser fileChooser = new FileChooser();

          //Set extension filter
          FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
          fileChooser.getExtensionFilters().add(extFilter);

          //Show save file dialog
          File file = fileChooser.showSaveDialog(primaryStage);
          System.out.println("file is " + file.getName());

      }
  });

Also how do I set what is actually being saved?

Was it helpful?

Solution

Where from should the file chooser know what to write into your file. You need to open a stream on the returned file (e.g. a FileInputStream) and write the information out yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top