0

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?

  • Possible duplicate of [How to add CheckBox's to a TableView in JavaFX](https://stackoverflow.com/questions/7217625/how-to-add-checkboxs-to-a-tableview-in-javafx) – user1803551 Jul 30 '18 at 19:48
  • 1
    unrelated: better not name custom classes the same as core classes ... sooner or later you will confuse yourself ;) – kleopatra Jul 31 '18 at 08:10

1 Answers1

1

By default, a TableView is not editable meaning any custom controls within the TableView are also disabled.

You can enable editing by either updating your FXML definition of the TableView to include editable="true" or in your controller with tableView.setEditable(true);.

Zephyr
  • 9,885
  • 4
  • 28
  • 63