Saturday, March 28, 2015

JavaFX example: save TextArea to file, using FileChooser

JavaFX example show how to get content from TextArea, and save it as txt file using FileChooser.


package javafxsavetext;

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.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXSaveText extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("java-buddy.blogspot.com");
        Group root = new Group();
        
        TextArea textArea = new TextArea();

        Button buttonSave = new Button("Save");
                
        buttonSave.setOnAction((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(textArea.getText(), file);
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(textArea, buttonSave);
        
        root.getChildren().add(vBox);
        
        primaryStage.setScene(new Scene(root, 500, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private void SaveFile(String content, File file){
        try {
            FileWriter fileWriter;
             
            fileWriter = new FileWriter(file);
            fileWriter.write(content);
            fileWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(JavaFXSaveText.class
                .getName()).log(Level.SEVERE, null, ex);
        }
         
    }
    
}


Related:
- Read text file with JavaFX FileChooser

1 comment:

  1. Hello I'm using MVC format and I Have my file chooser in my controller. I have one for my open file button and the second for my save file button. when I try to save text from textArea my file chooser still just shows the cancel and open button and wont save file from textArea (terminalOut). what might I be Doing Wrong???

    ReplyDelete