Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * My texteditor can open, close, clear, save, and save as.
- *
- * @author Henning Scharf
- */
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Menu;
- import javafx.scene.control.MenuBar;
- import javafx.scene.control.MenuItem;
- import javafx.scene.control.TextArea;
- import javafx.scene.layout.Priority;
- import javafx.scene.layout.VBox;
- import javafx.stage.FileChooser;
- import javafx.stage.Stage;
- public class TextEditor extends Application {
- // saves the path of my working file
- private static String lastSaved;
- public static void main(String[] args) {
- Application.launch(args);
- }
- @Override
- public void start(Stage primaryStage) throws Exception {
- primaryStage.setTitle("TextEditor");
- VBox vbox = new VBox(1.0);
- // 1 textarea
- TextArea txt = new TextArea("");
- // 1 Menubar
- final MenuBar menubar = new MenuBar();
- // I thought about only using 1 "File" because other programs handle it
- // similar
- Menu menuFile = new Menu("Datei");
- menubar.getMenus().addAll(menuFile);
- // Here i add my Menu Items
- MenuItem menuOpen1 = new MenuItem("Neu");
- MenuItem menuOpen2 = new MenuItem("Öffnen");
- MenuItem menuOpen3 = new MenuItem("Speichern");
- MenuItem menuOpen4 = new MenuItem("Speichern unter");
- MenuItem menuOpen5 = new MenuItem("Schließen");
- // now i add my items to "Datei"
- menuFile.getItems().addAll(menuOpen1, menuOpen2, menuOpen3, menuOpen4,
- menuOpen5);
- // this one resets my textarea
- menuOpen1.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent arg0) {
- txt.clear();
- lastSaved = null;
- }
- });
- //open
- menuOpen2.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent arg0) {
- txt.clear();
- FileChooser fchoose = new FileChooser();
- fchoose.setTitle("Open Menu");
- fchoose.setSelectedExtensionFilter(new FileChooser.ExtensionFilter(
- "*.txt", "*.log", "*.txt", "*.log"));
- File choosenFile = fchoose.showOpenDialog(primaryStage);
- if (choosenFile != null) {
- try {
- // read my text
- BufferedReader reader = new BufferedReader(
- new FileReader(choosenFile));
- // puffer string
- String puffer;
- try {
- while ((puffer = reader.readLine()) != null) {
- // add text and enters
- txt.appendText(puffer);
- txt.appendText(System.lineSeparator());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- // set my working file
- lastSaved = choosenFile.getAbsolutePath();
- }
- });
- //save
- menuOpen3.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent arg0) {
- // check if this is the first file used in this instance
- if (lastSaved != null) {
- FileWriter fWriter = null;
- try {
- // write my text in my file
- fWriter = new FileWriter(lastSaved);
- fWriter.write(txt.getText());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- // cant write without closing
- fWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // use save as if it is the first one
- } else {
- FileChooser fchoose = new FileChooser();
- fchoose.setTitle("Save as Menu");
- // setting file endings
- fchoose.setSelectedExtensionFilter(new FileChooser.ExtensionFilter(
- "*.txt", "*.log", "*.txt", "*.log"));
- // show file
- File choosenFile = fchoose.showSaveDialog(primaryStage);
- FileWriter fWriter = null;
- // checks if i have a txt ending already
- if (choosenFile != null
- && !choosenFile.getName().contains(".txt")) {
- choosenFile = new File(choosenFile + ".txt");
- }
- try {
- // if i have no working path so i will "save as"
- fWriter = new FileWriter(choosenFile);
- // writes
- fWriter.write(txt.getText());
- fWriter.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- // cant write without closing
- fWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- lastSaved = choosenFile.getAbsolutePath();
- }
- }
- });
- //save as
- menuOpen4.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent arg0) {
- FileChooser fchoose = new FileChooser();
- fchoose.setTitle("Save as Menu");
- // file endings
- fchoose.setSelectedExtensionFilter(new FileChooser.ExtensionFilter(
- "*.txt", "*.log", "*.txt", "*.log"));
- // show file
- File choosenFile = fchoose.showSaveDialog(primaryStage);
- FileWriter fWriter = null;
- // checks if i have a txt ending already
- if (choosenFile != null
- && !choosenFile.getName().endsWith(".txt")) {
- choosenFile = new File(choosenFile.getName() + ".txt");
- }
- try {
- fWriter = new FileWriter(choosenFile);
- // write my text in my file
- fWriter.write(txt.getText());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- fWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // sets working path
- lastSaved = choosenFile.getAbsolutePath();
- }
- });
- // this one closes my Stage
- menuOpen5.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent arg0) {
- primaryStage.close();
- Platform.exit();
- }
- });
- // here i let my textarea grow with my window
- VBox.setVgrow(txt, Priority.ALWAYS);
- // this one adds everything to my box
- vbox.getChildren().addAll(menubar, txt);
- // this shows my box
- Scene scene = new Scene(vbox);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement