jeudi 26 avril 2018

How can I add a checkbox to a table that reads and writes the object property that it represents in JavaFX

I have a table that lists objects of type Bot which have a name and isOn properties that I want to list:

private SimpleStringProperty name;
private boolean isOn;

The boolean isOn, I want to be read from a checkbox and also editable from that checkbox

So far, I have been able to add a checkbox to a column in my table for each row but it is purely visual (i.e. it is not tied to the Bot's isOn member).

How can I make the checkbox read and write from and to this member of Bot?

Here is my code dealing with the Table altogether:

ObservableList<Bot> bots = FXCollections.observableArrayList();
@FXML
private TableView<Bot> botTable;
@FXML
private TableColumn<Bot, String> nameColumn;
@FXML
private TableColumn<Bot, Boolean> statusColumn;

public void initialize(URL location, ResourceBundle resources){
    nameColumn.setCellValueFactory(new PropertyValueFactory<Bot, String>("name"));
    statusColumn.setCellValueFactory(new PropertyValueFactory<Bot, Boolean>("on"));
    statusColumn.setSortable(false);

    statusColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Bot, Boolean>, ObservableValue<Boolean>>(){
        @Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Bot, Boolean> features) {
            return new SimpleBooleanProperty(features.getValue() != null);
        }
    });

    // create a cell value factory with an add button for each row in the table.
    statusColumn.setCellFactory(new Callback<TableColumn<Bot, Boolean>, TableCell<Bot, Boolean>>() {
        @Override public TableCell<Bot, Boolean> call(TableColumn<Bot, Boolean> personBooleanTableColumn) {
            return new AddBotCell(/*stage, botTable*/);
        }
    });

    botTable.setItems(bots);
}

/** A table cell containing a button for adding a new person. */
private class AddBotCell extends TableCell<Bot, Boolean> {
    // a button for adding a new person.
    final CheckBox checkbox = new CheckBox();
    // pads and centers the add button in the cell.
    final StackPane paddedCheckBox = new StackPane();
    // records the y pos of the last button press so that the add person dialog can be shown next to the cell.
    final DoubleProperty buttonY = new SimpleDoubleProperty();

    AddBotCell(/*final Stage stage, final TableView table*/) {
        paddedCheckBox.setPadding(new Insets(3));
        paddedCheckBox.getChildren().add(checkbox);
        checkbox.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
            }
        });
    }
    /** places an add button in the row only if the row is not empty. */
    @Override protected void updateItem(Boolean item, boolean empty) {
        super.updateItem(item, empty);
        if (!empty) {
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            setGraphic(checkbox);
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire