I created a table view, linked it with my model and put into fxml like this:
Main.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("table.fxml"));
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
public class Controller {
@FXML
private javafx.scene.control.TableView<TableView> tableView;
@FXML
public void initialize() {
ObservableList<TableView> data = tableView.getItems();
data.add(new TableView("Item1", true));
data.add(new TableView("Item2", false));
}
}
TableView.java
public class TableView {
private SimpleStringProperty text;
private SimpleBooleanProperty check;
TableView(String text, boolean check) {
this.text = new SimpleStringProperty(text);
this.check = new SimpleBooleanProperty(check);
}
public boolean getCheck() {
return check.get();
}
public SimpleBooleanProperty checkProperty() {
return check;
}
public void setCheck(boolean check) {
this.check.set(check);
}
public String getText() {
return text.get();
}
public SimpleStringProperty textProperty() {
return text;
}
public void setText(String text) {
this.text.set(text);
}
}
CheckBoxTableCellFactory.java
public class CheckBoxTableCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
public TableCell<S, T> call(TableColumn<S, T> param) {
return new CheckBoxTableCell<>();
}
}
table.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import table.CheckBoxTableCellFactory?>
<VBox prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="table.Controller">
<SplitPane focusTraversable="true" VBox.vgrow="ALWAYS">
<Pane prefHeight="200.0" prefWidth="200.0">
<TableView fx:id="tableView" layoutX="6.0" layoutY="4.0" prefHeight="406.0" prefWidth="694.0">
<columns>
<TableColumn prefWidth="75.0" text="Check">
<cellValueFactory>
<PropertyValueFactory property="check" />
</cellValueFactory>
<cellFactory>
<CheckBoxTableCellFactory />
</cellFactory>
</TableColumn>
<TableColumn prefWidth="200.0" text="Text">
<cellValueFactory>
<PropertyValueFactory property="text"/>
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</Pane>
</SplitPane>
</VBox>
I looks good - screenshot
but checkboxes are disabled - I cannot check/un-check them. When I click on them, they don't change their state (checked/un-checked). How to fix it?