Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fileIO;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.*;
- import javafx.stage.Stage;
- import java.util.*;
- import java.io.*;
- import javafx.geometry.Pos;
- public class FileInput extends Application {
- Button btnOK = new Button("Save");
- Label lblLName = new Label("Last Name:");
- Label lblFName = new Label("First Name:");
- Label lblCourse = new Label("Course: ");
- Label lblTerm = new Label("Term: ");
- TextField txtLName = new TextField();
- TextField txtFName = new TextField();
- TextField txtCourse = new TextField();
- TextField txtTerm = new TextField();
- TextArea txtaOutput = new TextArea();
- @Override
- public void start(Stage primaryStage) {
- btnOK.setOnAction(e -> {
- try
- {
- String appendString = "";
- PrintWriter fileOut = new PrintWriter(new FileOutputStream("src/fileIO/testdata.txt",true));
- appendString += txtLName.getText() + "\t" +
- txtFName.getText() + "\t" +
- txtCourse.getText() + "\t" +
- txtTerm.getText() + "\n";
- fileOut.append(appendString);
- fileOut.close();
- fillOutput();
- }
- catch (Exception error)
- {
- txtaOutput.setText(error.toString());
- }
- });
- GridPane primaryPane = new GridPane();
- GridPane rightPane = new GridPane();
- GridPane containerPane = new GridPane();
- primaryPane.setAlignment(Pos.CENTER);
- primaryPane.add(lblLName, 0, 0);
- primaryPane.add(txtLName, 1, 0);
- primaryPane.add(lblFName, 0, 1);
- primaryPane.add(txtFName, 1, 1);
- primaryPane.add(lblCourse, 0, 2);
- primaryPane.add(txtCourse, 1, 2);
- primaryPane.add(lblTerm, 0, 3);
- primaryPane.add(txtTerm, 1, 3);
- primaryPane.add(btnOK,1,4);
- rightPane.add(txtaOutput, 0, 0);
- containerPane.setAlignment(Pos.CENTER);
- containerPane.add(primaryPane, 0, 0);
- containerPane.add(rightPane, 1, 0);
- Scene primaryScene = new Scene(containerPane,700,300);
- primaryStage.setTitle("File Input/Output");
- primaryStage.setScene(primaryScene);
- primaryStage.show();
- fillOutput();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- public void fillOutput()
- {
- txtaOutput.setText("");
- try
- {
- Scanner fileIn = new Scanner(new FileInputStream("src/fileIO/testdata.txt"));
- while (fileIn.hasNext())
- {
- txtaOutput.appendText(fileIn.next() + " " +
- fileIn.next() + " " +
- fileIn.next() + " " +
- fileIn.next() + "\n");
- }
- fileIn.close();
- }
- catch (Exception error)
- {
- System.out.println(error.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement