I am working with JavaFX and SceneBuilder, i created a TableView in SceneBuilder with 2 Columns, one for the task and is supposed to be a checkbox that the user can check when he did a task.
I'm also adding the Table to a MySql Database and initializing the values of the databank when the user starts the application. everything works fine with the initializing, the task shows on start, but i can't figure out how to add a checkbox to the column cell and how to initialize it then on start.
package org.stephan.javafx_todolist;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import org.stephan.javafx_todolist.data.DBConnection;
import java.io.IOException;
import java.sql.*;
import java.util.Objects;
public class HelloController {
@FXML
private AnchorPane mainPane;
@FXML
private TableView<Task> tvTaskTable;
@FXML
private TableColumn<Task, String> taskColumn;
@FXML
private TableColumn<Task, Boolean> isDoneColumn;
ObservableList<Task> tasks = FXCollections.observableArrayList();
@FXML
protected void onAddButtonClick() throws IOException {
Stage stage = (Stage) mainPane.getScene().getWindow();
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("add-scene.fxml")));
Scene scene = new Scene(root);
stage.setScene(scene);
}
@FXML
public void initialize() throws SQLException {
tvTaskTable.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
taskColumn.setCellValueFactory(new PropertyValueFactory<>("taskName"));
isDoneColumn.setCellValueFactory(new PropertyValueFactory<>("isDone"));
DBConnection dbConnection = new DBConnection("jdbc:mysql://localhost/javafx", "root", "");
String query = "select * from todos";
try (Connection connection = DriverManager.getConnection(dbConnection.dbUrl(), dbConnection.user(), dbConnection.password());
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
tasks.add(new Task(resultSet.getString("task"), false));
}
} catch (SQLException e) {
System.out.println("Error");
}
tvTaskTable.setItems(tasks);
}
}