Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. package fileIO;
  2.  
  3. import javafx.application.Application;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.*;
  8. import javafx.scene.layout.*;
  9. import javafx.stage.Stage;
  10. import java.util.*;
  11. import java.io.*;
  12. import javafx.geometry.Pos;
  13.  
  14. public class FileInput extends Application {
  15. Button btnOK = new Button("Save");
  16. Label lblLName = new Label("Last Name:");
  17. Label lblFName = new Label("First Name:");
  18. Label lblCourse = new Label("Course: ");
  19. Label lblTerm = new Label("Term: ");
  20. TextField txtLName = new TextField();
  21. TextField txtFName = new TextField();
  22. TextField txtCourse = new TextField();
  23. TextField txtTerm = new TextField();
  24. TextArea txtaOutput = new TextArea();
  25.  
  26. @Override
  27. public void start(Stage primaryStage) {
  28.  
  29. btnOK.setOnAction(e -> {
  30. try
  31. {
  32. String appendString = "";
  33. PrintWriter fileOut = new PrintWriter(new FileOutputStream("src/fileIO/testdata.txt",true));
  34. appendString += txtLName.getText() + "\t" +
  35. txtFName.getText() + "\t" +
  36. txtCourse.getText() + "\t" +
  37. txtTerm.getText() + "\n";
  38.  
  39. fileOut.append(appendString);
  40. fileOut.close();
  41.  
  42. fillOutput();
  43. }
  44. catch (Exception error)
  45. {
  46. txtaOutput.setText(error.toString());
  47. }
  48.  
  49.  
  50. });
  51.  
  52. GridPane primaryPane = new GridPane();
  53. GridPane rightPane = new GridPane();
  54. GridPane containerPane = new GridPane();
  55. primaryPane.setAlignment(Pos.CENTER);
  56.  
  57. primaryPane.add(lblLName, 0, 0);
  58. primaryPane.add(txtLName, 1, 0);
  59. primaryPane.add(lblFName, 0, 1);
  60. primaryPane.add(txtFName, 1, 1);
  61. primaryPane.add(lblCourse, 0, 2);
  62. primaryPane.add(txtCourse, 1, 2);
  63. primaryPane.add(lblTerm, 0, 3);
  64. primaryPane.add(txtTerm, 1, 3);
  65. primaryPane.add(btnOK,1,4);
  66.  
  67. rightPane.add(txtaOutput, 0, 0);
  68.  
  69. containerPane.setAlignment(Pos.CENTER);
  70. containerPane.add(primaryPane, 0, 0);
  71. containerPane.add(rightPane, 1, 0);
  72.  
  73. Scene primaryScene = new Scene(containerPane,700,300);
  74. primaryStage.setTitle("File Input/Output");
  75. primaryStage.setScene(primaryScene);
  76. primaryStage.show();
  77.  
  78. fillOutput();
  79.  
  80. }
  81.  
  82. /**
  83. * @param args the command line arguments
  84. */
  85. public static void main(String[] args) {
  86. launch(args);
  87. }
  88.  
  89. public void fillOutput()
  90. {
  91. txtaOutput.setText("");
  92. try
  93. {
  94. Scanner fileIn = new Scanner(new FileInputStream("src/fileIO/testdata.txt"));
  95. while (fileIn.hasNext())
  96. {
  97. txtaOutput.appendText(fileIn.next() + " " +
  98. fileIn.next() + " " +
  99. fileIn.next() + " " +
  100. fileIn.next() + "\n");
  101.  
  102. }
  103. fileIn.close();
  104. }
  105. catch (Exception error)
  106. {
  107. System.out.println(error.toString());
  108. }
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement