Saturday, May 12, 2012

Save file with JavaFX FileChooser

javafx.stage.FileChooser provides support for standard file dialogs. The showSaveDialog(Window ownerWindow) method shows a new file save dialog. The method doesn't return until the displayed file save dialog is dismissed. The return value specifies the file chosen by the user or null if no selection has been made. If the owner window for the file dialog is set, input to all windows in the dialog's owner chain is blocked while the file dialog is being shown.

Example of saving file with JavaFX FileChooser:



package javafx_text;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Text extends Application {
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 launch(args);
 }
 
 @Override
 public void start(final Stage primaryStage) {
 primaryStage.setTitle("java-buddy.blogspot.com");
 Group root = new Group();
 
 final String Santa_Claus_Is_Coming_To_Town =
 "You better watch out\n"
 + "You better not cry\n"
 + "Better not pout\n"
 + "I'm telling you why\n"
 + "Santa Claus is coming to town\n"
 + "\n"
 + "He's making a list\n"
 + "And checking it twice;\n"
 + "Gonna find out Who's naughty and nice\n"
 + "Santa Claus is coming to town\n"
 + "\n"
 + "He sees you when you're sleeping\n"
 + "He knows when you're awake\n"
 + "He knows if you've been bad or good\n"
 + "So be good for goodness sake!\n"
 + "\n"
 + "O! You better watch out!\n"
 + "You better not cry\n"
 + "Better not pout\n"
 + "I'm telling you why\n"
 + "Santa Claus is coming to town\n"
 + "Santa Claus is coming to town\n";
 
 Text textSong = TextBuilder.create()
 .text(Santa_Claus_Is_Coming_To_Town)
 .build(); 
 
 Button buttonSave = ButtonBuilder.create()
 .text("Save")
 .build();
 
 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);
 
 if(file != null){
 SaveFile(Santa_Claus_Is_Coming_To_Town, file);
 }
 }
 });
 
 VBox vBox = VBoxBuilder.create()
 .children(textSong, buttonSave)
 .build();
 
 root.getChildren().add(vBox);
 
 primaryStage.setScene(new Scene(root, 500, 400));
 primaryStage.show();
 }
 
 private void SaveFile(String content, File file){
 try {
 FileWriter fileWriter = null;
 
 fileWriter = new FileWriter(file);
 fileWriter.write(content);
 fileWriter.close();
 } catch (IOException ex) {
 Logger.getLogger(JavaFX_Text.class.getName()).log(Level.SEVERE, null, ex);
 }
 
 }
}


Related:
- Read text file with JavaFX FileChooser
- Save TextArea to file, using FileChooser

13 comments:

  1. Thank you! This information helps me !:)

    Reply Delete
  2. Thanks Man...!! I just needed that only :)

    Reply Delete
  3. and how to save the content of a text area in a text file?

    Reply Delete
    Replies
    1. what if you wanted to edit the text in the file(song) how could you save the edits that youve done

      Delete
    2. what if you wanted to make an edit to the textfile, how could you save that edit

      Delete
  4. thank you , that's help me

    Reply Delete
  5. good tuto but I want to transfert my data from table to another in oracle deleting the first data u=in first table

    Reply Delete

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /