Guest User

Untitled

a guest
Jun 1st, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. package sample;
  2.  
  3. import java.net.URL;
  4. import java.sql.Connection;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.time.LocalDate;
  8. import java.util.Observable;
  9. import java.util.ResourceBundle;
  10.  
  11. import javafx.collections.FXCollections;
  12. import javafx.collections.ObservableList;
  13. import javafx.event.ActionEvent;
  14. import javafx.event.Event;
  15. import javafx.fxml.FXMLLoader;
  16. import javafx.fxml.Initializable;
  17. import javafx.fxml.FXML;
  18. import javafx.scene.Parent;
  19. import javafx.scene.Scene;
  20. import javafx.scene.control.Button;
  21. import javafx.scene.control.Label;
  22. import javafx.scene.control.TableColumn;
  23. import javafx.scene.control.TableView;
  24. import javafx.scene.control.cell.PropertyValueFactory;
  25. import javafx.stage.Stage;
  26.  
  27. public class Controller implements Initializable {
  28.  
  29.  
  30. @FXML Button mainAddButton;
  31. @FXML Button mainDeleteButton;
  32. @FXML Button mainEditButton;
  33. @FXML Button mainCloseButton;
  34.  
  35. @FXML TableView<Task> taskTableView;
  36. @FXML private TableColumn<Task, LocalDate> createDateColumn;
  37. @FXML private TableColumn<Task, LocalDate> dueDateColumn;
  38. @FXML private TableColumn<Task, String> descriptionColumn;
  39.  
  40. private DBConnection dbc;
  41.  
  42. Connection conn = null;
  43.  
  44. protected ObservableList<Task> tasks = FXCollections.observableArrayList();
  45.  
  46. //Button handling
  47. @FXML private void mainAddHandle(ActionEvent Event) {
  48. try {
  49. FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("addTask.fxml"));
  50. Parent root = (Parent) fxmlLoader.load();
  51. Stage stage = new Stage();
  52. stage.setTitle("Add Task");
  53. stage.setScene(new Scene(root));
  54. stage.setResizable(false);
  55. stage.show();
  56.  
  57. } catch (Exception e) {
  58. System.out.println("Couldn't open new window");
  59. }
  60. }
  61.  
  62. @FXML private void mainEditHandle(ActionEvent Event) {
  63. try {
  64. FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("editTask.fxml"));
  65. Parent root = (Parent) fxmlLoader.load();
  66. Stage stage = new Stage();
  67. stage.setTitle("Update your task");
  68. stage.setScene(new Scene(root));
  69. stage.setResizable(false);
  70. stage.show();
  71.  
  72. } catch (Exception e) {
  73. System.out.println("Couldn't open new window");
  74. }
  75. }
  76.  
  77. @FXML private void mainDeleteHandle(ActionEvent Event) {
  78. try {
  79.  
  80.  
  81. } catch (Exception e) {
  82. System.out.println("Couldn't delete task");
  83. }
  84. }
  85.  
  86. @FXML private void mainCloseHandle(ActionEvent Event) {
  87. try {
  88. Stage stage = (Stage) mainCloseButton.getScene().getWindow();
  89. stage.close();
  90. } catch (Exception e) {
  91. System.out.println("Couldn't close window");
  92. }
  93. }
  94.  
  95.  
  96. @Override
  97. public void initialize(URL url, ResourceBundle rb) {
  98.  
  99. createDateColumn.setCellValueFactory(new PropertyValueFactory<Task, LocalDate>("createDate"));
  100. dueDateColumn.setCellValueFactory(new PropertyValueFactory<Task, LocalDate>("dueDate"));
  101. descriptionColumn.setCellValueFactory(new PropertyValueFactory<Task, String>("description"));
  102.  
  103. loadDataFromDatabase(new ActionEvent());
  104. }
  105.  
  106.  
  107. @FXML
  108. public void loadDataFromDatabase(ActionEvent Event) {
  109. tasks.clear();
  110. try {
  111. conn = DBConnection.getConnection();
  112. ResultSet rs = conn.createStatement().executeQuery("select * from todo_list");
  113.  
  114. while(rs.next()) {
  115. tasks.add(new Task(rs.getDate("createDate").toLocalDate(),
  116. rs.getDate("dueDate").toLocalDate(),
  117. rs.getString("description")));
  118. }
  119.  
  120. } catch (SQLException ex) {
  121. System.out.println("Couldn't load data from database");
  122. }
  123. taskTableView.setItems(tasks);
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment