Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Last active December 21, 2021 11:41
Show Gist options
  • Save Da9el00/f75565592ded943420969a3fbcad4d2a to your computer and use it in GitHub Desktop.
Save Da9el00/f75565592ded943420969a3fbcad4d2a to your computer and use it in GitHub Desktop.
File Chooser
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
public class Controller implements Initializable {
FileChooser fileChooser = new FileChooser();
@FXML
private TextArea textArea;
//Added null check to check rather a file is picked or not
@FXML
void getText(MouseEvent event) {
File file = fileChooser.showOpenDialog(new Stage());
if(file != null){
try {
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine()){
textArea.appendText(scanner.nextLine() + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
@FXML
void save(MouseEvent event) {
File file = fileChooser.showSaveDialog(new Stage());
if(file != null){
saveSystem(file, textArea.getText());
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
fileChooser.setInitialDirectory(new File("C:\\Software\\FileChooser\\src\\sample"));
}
public void saveSystem(File file, String content){
try {
PrintWriter printWriter = new PrintWriter(file);
printWriter.write(content);
printWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
File file = new File("src\\sample\\text.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button layoutX="79.0" layoutY="93.0" mnemonicParsing="false" onMouseClicked="#getText" prefHeight="66.0" prefWidth="144.0" text="GetText" />
<TextArea fx:id="textArea" layoutX="300.0" layoutY="93.0" prefHeight="215.0" prefWidth="242.0" />
<Button layoutX="89.0" layoutY="214.0" mnemonicParsing="false" onMouseClicked="#save" prefHeight="78.0" prefWidth="125.0" text="Save" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment