You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.1 KiB

2 years ago
  1. package com.example.javafx;
  2. import javafx.collections.FXCollections;
  3. import javafx.fxml.FXML;
  4. import javafx.fxml.Initializable;
  5. import javafx.scene.control.TableColumn;
  6. import javafx.scene.control.TableView;
  7. import javafx.scene.control.cell.PropertyValueFactory;
  8. import java.io.*;
  9. import java.net.URL;
  10. import java.util.ResourceBundle;
  11. public class StudentController implements Initializable {
  12. @FXML
  13. private TableView<StudentModel> tbData;
  14. @FXML
  15. public TableColumn<StudentModel, Integer> studentId;
  16. @FXML
  17. public TableColumn<StudentModel, String> firstName;
  18. @FXML
  19. public TableColumn<StudentModel, Integer> studentAge;
  20. @Override
  21. public void initialize(URL url, ResourceBundle resources) {
  22. File file = new File("C:\\ADA\\FichPersonas.dat");
  23. tbData = new TableView<>();
  24. try {
  25. leerFichData(file, tbData);
  26. studentId.setCellValueFactory(new PropertyValueFactory<>("studentId"));
  27. firstName.setCellValueFactory(new PropertyValueFactory<>("firstName"));
  28. studentAge.setCellValueFactory(new PropertyValueFactory<>("studentAge"));
  29. } catch (IOException e) {
  30. throw new RuntimeException(e);
  31. }
  32. /*
  33. new StudentModel(1,"Juan","Martínez"),
  34. new StudentModel(2,"Perico","López"),
  35. new StudentModel(3,"Andrés","Gomez")
  36. ));*/
  37. }
  38. public static void leerFichData(File fichero, TableView<StudentModel> tbData) throws IOException {
  39. FileInputStream filein = new FileInputStream(fichero);
  40. DataInputStream dataIS = new DataInputStream(filein);
  41. int i;
  42. String n;
  43. int e;
  44. try {
  45. while (true) {
  46. i = dataIS.readInt();
  47. n = dataIS.readUTF();
  48. e = dataIS.readInt();
  49. tbData.setItems(FXCollections.observableArrayList(
  50. new StudentModel(i,n,e)));
  51. }
  52. }catch (EOFException error){
  53. System.out.println("error");
  54. }
  55. dataIS.close();
  56. }
  57. }