Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.time.LocalDate;
- import java.util.Observable;
- import java.util.ResourceBundle;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.event.Event;
- import javafx.fxml.FXMLLoader;
- import javafx.fxml.Initializable;
- import javafx.fxml.FXML;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.stage.Stage;
- public class Controller implements Initializable {
- @FXML Button mainAddButton;
- @FXML Button mainDeleteButton;
- @FXML Button mainEditButton;
- @FXML Button mainCloseButton;
- @FXML TableView<Task> taskTableView;
- @FXML private TableColumn<Task, LocalDate> createDateColumn;
- @FXML private TableColumn<Task, LocalDate> dueDateColumn;
- @FXML private TableColumn<Task, String> descriptionColumn;
- private DBConnection dbc;
- Connection conn = null;
- protected ObservableList<Task> tasks = FXCollections.observableArrayList();
- //Button handling
- @FXML private void mainAddHandle(ActionEvent Event) {
- try {
- FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("addTask.fxml"));
- Parent root = (Parent) fxmlLoader.load();
- Stage stage = new Stage();
- stage.setTitle("Add Task");
- stage.setScene(new Scene(root));
- stage.setResizable(false);
- stage.show();
- } catch (Exception e) {
- System.out.println("Couldn't open new window");
- }
- }
- @FXML private void mainEditHandle(ActionEvent Event) {
- try {
- FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("editTask.fxml"));
- Parent root = (Parent) fxmlLoader.load();
- Stage stage = new Stage();
- stage.setTitle("Update your task");
- stage.setScene(new Scene(root));
- stage.setResizable(false);
- stage.show();
- } catch (Exception e) {
- System.out.println("Couldn't open new window");
- }
- }
- @FXML private void mainDeleteHandle(ActionEvent Event) {
- try {
- } catch (Exception e) {
- System.out.println("Couldn't delete task");
- }
- }
- @FXML private void mainCloseHandle(ActionEvent Event) {
- try {
- Stage stage = (Stage) mainCloseButton.getScene().getWindow();
- stage.close();
- } catch (Exception e) {
- System.out.println("Couldn't close window");
- }
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- createDateColumn.setCellValueFactory(new PropertyValueFactory<Task, LocalDate>("createDate"));
- dueDateColumn.setCellValueFactory(new PropertyValueFactory<Task, LocalDate>("dueDate"));
- descriptionColumn.setCellValueFactory(new PropertyValueFactory<Task, String>("description"));
- loadDataFromDatabase(new ActionEvent());
- }
- @FXML
- public void loadDataFromDatabase(ActionEvent Event) {
- tasks.clear();
- try {
- conn = DBConnection.getConnection();
- ResultSet rs = conn.createStatement().executeQuery("select * from todo_list");
- while(rs.next()) {
- tasks.add(new Task(rs.getDate("createDate").toLocalDate(),
- rs.getDate("dueDate").toLocalDate(),
- rs.getString("description")));
- }
- } catch (SQLException ex) {
- System.out.println("Couldn't load data from database");
- }
- taskTableView.setItems(tasks);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment