JavaFX8 file writing (TextField)

1. Specifications

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

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.TextField;
import javafx.stage.Modality;

public class MainController {

	@FXML
	private ResourceBundle resources;

	@FXML
	private URL location;

	@FXML
	private TextField textField;

	@FXML
	private Button writeButton;

	@FXML
	void OnclickedWriteButton(ActionEvent event) throws IOException {
		String text = textField.getText();
		if (!text.isEmpty()) {
			FileWrite a = new FileWrite();
			a.write(text);
			textField.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 textField != null : "fx:id=\"textField\" was not injected: check your FXML file 'screen.fxml'.";
		assert writeButton != null : "fx:id=\"writeButton\" was not injected: check your FXML file 'screen.fxml'.";
		textField.setText("");
	}
}

3.4. screen.fxml

screen.fxml


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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?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>
      <TextField fx:id="textField" layoutX="155.0" layoutY="187.0" />
      <Button fx:id="writeButton" layoutX="364.0" layoutY="187.0" mnemonicParsing="false" onAction="#OnclickedWriteButton" prefHeight="27.0" prefWidth="80.0" text="writing" />
   </children>
</Pane>

Recommended Posts

JavaFX8 file writing (TextField)
JavaFX8 file writing (TextArea)
Implement textField focus-out event in JavaFX
[Personal memo] Writing a file using BufferedWriter
JavaFX application file structure (FXML, CSS, properties)