JavaFX8 file writing (TextArea)

1. Specifications

By entering characters and pressing the button, the entered characters are written to the file. Use TextArea.

2. File structure

Create a project in Eclipse.

write ・ ・ ・ ・ ・ ・ (1) ├ test.txt ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ (2) └ src ・ ・ ・ ・ ・ ・ ・ ・ (3) └ pkg ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ (4) ├ FileWrite.java ・ ・ ・ ・ ・ ・ ・ (5) ├ Main.java ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ (6) ├ MainController.java ・ ・ ・ ・ (7) └ screen.fxml ・ ・ ・ ・ ・ ・ ・ ・ (8)

(1) Project to be created (2) Text file to be written (If the file does not exist, it is created at runtime, and if it exists, it is added) (3) Source file directory created by yourself when creating a project (4) Package to create (5) Source file (described in the next chapter), file writing (6) Source file (described in the next chapter), screen startup (7) Source file (described in the next chapter), screen controller (8) Source file (described in the next chapter, created by SceneBuilder)

3. Source file

3.1. FileWrite.java

FileWrite.java


package pkg;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileWrite {
	public void write(String text) throws IOException {
		try {
//Describe either, comment out one-----------------------------------
			//FileWriter file = new FileWriter("test.txt");//Overwrite
			FileWriter file = new FileWriter("test.txt", true);//Postscript
//---------------------------------------------------------------------
			//Create an object of PrintWriter class
			PrintWriter pw = new PrintWriter(new BufferedWriter(file));
			//Write to file
			pw.println(text);
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}

3.2. Main.java

Main.java


package pkg;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			//Loading the scene graph from FXML
			FXMLLoader loader = new FXMLLoader(getClass().getResource("screen.fxml"));
			Parent root = loader.load();

			//Creating a scene with the root node of the scene graph
			Scene scene = new Scene(root, 600, 400);
			//Setting the scene on the stage
			primaryStage.setScene(scene);
			primaryStage.setTitle("no title");
			primaryStage.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

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

3.3. MainController.java

MainController.java


package pkg;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Modality;

public class MainController {

	@FXML
	private ResourceBundle resources;

	@FXML
	private URL location;

	@FXML
	private TextArea textArea;

	@FXML
	private Button writeButton;

	@FXML
	void OnclickedWriteButton(ActionEvent event) throws IOException {
		String text = textArea.getText();
		if (!text.isEmpty()) {
			FileWrite a = new FileWrite();
			a.write(text);
			textArea.setText("");
		} else {
			Alert alert = new Alert(AlertType.INFORMATION); //Set an alert type called information
			alert.initModality(Modality.WINDOW_MODAL); //You cannot operate other windows except for some windows until the operation is completed.
			alert.setTitle("warning"); //Set title
			alert.setContentText("No text has been entered."); //Set the content
			alert.showAndWait(); //Display an alert and wait for execution until closed
		}
	}

	@FXML
	void initialize() {
		assert textArea != null : "fx:id=\"textArea\" was not injected: check your FXML file 'screen.fxml'.";
		assert writeButton != null : "fx:id=\"writeButton\" was not injected: check your FXML file 'screen.fxml'.";
		textArea.setText("");
	}
}

3.4. screen.fxml

screen.fxml


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pkg.MainController">
   <children>
      <Button fx:id="writeButton" layoutX="260.0" layoutY="340.0" mnemonicParsing="false" onAction="#OnclickedWriteButton" prefHeight="27.0" prefWidth="80.0" text="writing" />
      <TextArea fx:id="textArea" layoutX="53.0" layoutY="81.0" prefHeight="239.0" prefWidth="494.0" />
   </children>
</Pane>

Recommended Posts

JavaFX8 file writing (TextArea)
JavaFX8 file writing (TextField)
[Personal memo] Writing a file using BufferedWriter
JavaFX application file structure (FXML, CSS, properties)