write data to a file gui javafx

Solutions on MaxInterview for write data to a file gui javafx by the best coders in the world

showing results for - "write data to a file gui javafx"
Celia
03 Aug 2016
1package com.fileio;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import javafx.application.Application;
7import javafx.event.ActionEvent;
8import javafx.event.EventHandler;
9import javafx.scene.Scene;
10import javafx.scene.control.Button;
11import javafx.scene.control.Label;
12import javafx.scene.control.TextField;
13import javafx.scene.layout.BorderPane;
14import javafx.stage.Stage;
15
16/**
17 *
18 * @author mpawlan
19 */
20public class FileIO extends Application {
21
22    TextField textField;
23    Label text, clicked;
24    Button button, clickButton;
25    BorderPane BPane;
26    private boolean _clickMeMode = true;
27
28    @Override
29    public void start (Stage primaryStage) { 
30    //Create GridPane
31    BPane = new BorderPane();
32    BPane.setId("grid-pane   BPane.getStyleClass().add("pane-styles");
33
34    //Create Scene and add Grid
35    Scene Scene = new Scene(BPane, 300, 200);
36            Scene.getStylesheets().add(this.getClass().getResource(
37                    "EssentialsJPL.css").toExternalForm 
38    //Create the stage and add the scene
39    primaryStage.setTitle("FileIO Application");
40    primaryStage.setScene(Scene);
41
42    text = new Label("Text to save to file:");
43    textField = new TextField();
44    textField.setMaxWidth(200);
45    button = new Button("Click Me");
46    button.setOnAction(new EventHandler<ActionEvent>() {
47
48    @Override
49    public void handle(ActionEvent event) {
50        Object source = event.getSource();
51        String s = null;
52        //Variable to display text read from file
53        if (_clickMeMode) {
54            FileInputStream in = null;
55            FileOutputStream out = null;
56            try {
57            //Code to write to file
58            String text = textField.getText();
59            byte b[] = text.getBytes();
60            String outputFileName = System.getProperty("user.home",
61            File.separatorChar + "home"
62                + File.separatorChar + "monica")
63                + File.separatorChar + "text.txt";
64                out = new FileOutputStream(outputFileName);
65                out.write(b);
66                out.close();
67                //Clear text field
68                textField.setText("");
69                //Code to read from file
70                String inputFileName = System.getProperty("user.home",
71                    File.separatorChar + "home"
72                    + File.separatorChar + "monica")
73                    + File.separatorChar + "text.txt";
74                File inputFile = new File(inputFileName);
75                in = new FileInputStream(inputFile);
76                byte bt[] = new byte[(int) inputFile.length()];
77                in.read(bt);
78                s = new String(bt);
79                in.close();
80            } catch (java.io.IOException e) {
81                    System.out.println("Cannotss text.txt");
82            } finally {
83                try {
84                    in.close();
85                    out.close();
86                } catch (java.io.IOException e) {
87                    System.out.println("Cannote");
88                }
89            }
90            //Clear text field
91            textField.setText("");
92            //Display text read from file
93            text.setText("Text retrieved from file: \n\n" + s);
94            BPane.getChildren().remove(textField);
95            button.setText("Click Again");
96            _clickMeMode = false;
97        } else {
98            //Save text to file
99            text.setText("Text to save to file:");
100            BPane.getChildren().add(textField);
101            textField.setText("");
102            button.setText("Click Me");
103            _clickMeMode = true;
104        }
105    }
106    });
107
108    //Set positions for each control in the BorderPane
109    BPane.setTop(text); 
110    BPane.setCenter(textField);
111    BPane.setBottom(button);
112           
113    //Show the scene
114    primaryStage.show(); 
115} 
116    
117    //main method
118    public static void main(String[] args){
119        launch(args);
120    }
121}
122