Guest User

Untitled

a guest
Mar 9th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. public class ThirdWindowController implements Initializable {
  2. Database database = new Database();
  3.  
  4. private ObservableList<ObservableList> data;
  5.  
  6. @FXML
  7. MenuItem createCategories = new MenuItem();
  8.  
  9. @FXML
  10. private TableView tableOfProp;
  11.  
  12. @FXML
  13. private TableView nameAndCount;
  14.  
  15. @FXML
  16. Button addCategories = new Button();
  17.  
  18. @Override
  19. public void initialize(URL location, ResourceBundle resources) {
  20. try {
  21. database.setConnection();
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace();
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. Connection connection = null;
  28. Statement statement;
  29. data = FXCollections.observableArrayList();
  30. try {
  31. Class.forName("org.sqlite.JDBC");
  32. connection = DriverManager.getConnection("jdbc:sqlite:Database.db");
  33.  
  34. statement = connection.createStatement();
  35. ResultSet rs = statement.executeQuery("select category from categories");
  36.  
  37. for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
  38. //We are using non property style for making dynamic table
  39. final int j = i;
  40. TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
  41. col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
  42. public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
  43. return new SimpleStringProperty(param.getValue().get(j).toString());
  44. }
  45. });
  46.  
  47. tableOfProp.getColumns().addAll(col);
  48. System.out.println("Column [" + i + "] ");
  49. }
  50. /********************************
  51. * Data added to ObservableList *
  52. ********************************/
  53. while (rs.next()) {
  54. //Iterate Row
  55. ObservableList<String> row = FXCollections.observableArrayList();
  56. for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  57. //Iterate Column
  58. row.add(rs.getString(i));
  59. }
  60. System.out.println("Row [1] added " + row);
  61. data.add(row);
  62. }
  63.  
  64. //FINALLY ADDED TO TableView
  65. tableOfProp.setItems(data)
  66. // insert(nameAndCount,"select name,amount from ingredients");
  67.  
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. System.out.println("Error on Building Data");
  71. }
  72.  
  73. createCategories.setOnAction((event) -> {
  74.  
  75. Parent root;
  76. try {
  77. FXMLLoader loader = new FXMLLoader(getClass().getResource("../fxFiles/FourthWindowFX.fxml"));
  78. root = loader.load();
  79. Stage stage = new Stage();
  80. stage.setTitle("Second window");
  81. stage.initModality(Modality.WINDOW_MODAL);
  82. Scene scene = new Scene(root);
  83. stage.setScene(scene);
  84. stage.initOwner(addCategories.getScene().getWindow());
  85. stage.showAndWait();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. });
  90. // todo for button
  91. }
  92.  
  93. public void createWindow() {
  94. Parent root;
  95. try {
  96. FXMLLoader loader = new FXMLLoader(getClass().getResource("../fxFiles/ThirdWindowFX.fxml"));
  97. root = loader.load();
  98.  
  99. Stage stage = new Stage();
  100. stage.setTitle("Second window");
  101. stage.initModality(Modality.WINDOW_MODAL);
  102.  
  103. Scene scene = new Scene(root);
  104. stage.setScene(scene);
  105. stage.showAndWait();
  106. database.closeConnection();
  107. } catch (Exception exc) {
  108. exc.printStackTrace();
  109. }
  110. }
  111.  
  112. public void insert(TableView tableView, String sqlStatement) throws
  113. SQLException, ClassNotFoundException {
  114. Connection connection = null;
  115. Statement statement;
  116. data = FXCollections.observableArrayList();
  117. try {
  118. Class.forName("org.sqlite.JDBC");
  119. connection = DriverManager.getConnection("jdbc:sqlite:Database.db");
  120.  
  121. statement = connection.createStatement();
  122. ResultSet rs = statement.executeQuery(sqlStatement);
  123.  
  124. for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
  125. //We are using non property style for making dynamic table
  126. final int j = i;
  127. TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
  128. col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
  129. public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
  130. return new SimpleStringProperty(param.getValue().get(j).toString());
  131. }
  132. });
  133.  
  134. tableView.getColumns().addAll(col);
  135. System.out.println("Column [" + i + "] ");
  136. }
  137. /********************************
  138. * Data added to ObservableList *
  139. ********************************/
  140. while (rs.next()) {
  141. //Iterate Row
  142. ObservableList<String> row = FXCollections.observableArrayList();
  143. for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  144. //Iterate Column
  145. row.add(rs.getString(i));
  146. }
  147. System.out.println("Row [1] added " + row);
  148. data.add(row);
  149. }
  150.  
  151. //FINALLY ADDED TO TableView
  152. tableView.setItems(data);
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. System.out.println("Error on Building Data");
  156. }
  157. }
  158. }
Add Comment
Please, Sign In to add comment