0

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?

user2033259
  • 29
  • 2
  • 9

1 Answers1

1

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.

tomsontom
  • 5,856
  • 2
  • 23
  • 21
  • 2
    Thank you. For some reason I thought the save button on the dialog will have a built in listener to do that. Silly me. – user2033259 Mar 28 '13 at 22:38