CheckBoxTableCell just show unchecked box

165 views Asked by At

I try to add CheckBoxTableCell to my TableColumn but the cell just show unchecked box regardless the value is true or false

My Object

public class Room {
    private String id,type,category,floor,number;
    private Boolean status;
    //setter and getter
    ........
}

How I declare the table

@FXML
private TableView<Room> roomTable;
@FXML
private TableColumn ....
@FXML
private TableColumn<Room,Boolean> statusColumn;
public void initialize(){
....
statusColumn.setCellValueFactory(new PropertyValueFactory<Room, Boolean>("status")); // here
statusColumn.setCellFactory(CheckBoxTableCell.forTableColumn(statusColumn));
....
}

I have solve this problem with making a new variable with BooleanProperty type and change the PropertyValueFactory parameter with the new variable
the problem is I didn't want to use BooleanProperty or Property
because all my model class still use the standard type not the Property
is there a way to do that?
if not maybe i just change all my model class variable to Property variable

2

There are 2 answers

1
James_D On

To keep the cell in sync with the property, the cell needs to be notified when the property value changes. This is the functionality the JavaFX property classes provide. There are ways to bind to classical Java Bean properties, but they involve a lot more "wiring" and will end up being more complicated than using JavaFX property classes. This question is similar.

0
javaHunter On

I would do it the using the following:

statusColumn.setCellValueFactory(new Callback<CellDataFeatures<Room, Boolean>, ObservableValue<Boolean>>() {
                @Override
                public ObservableValue<Boolean> call(CellDataFeatures<Room, Boolean> room) {
                    return new ReadOnlyBooleanWrapper(room.getValue().getStatus());
                }
            });
statusColumn.setCellFactory(CheckBoxTableCell.forTableColumn(statusColumn));

Now this works fine if your checkBox is not editable. Otherwise, you have to use Java Beans Properties to keep the checkBox and the status value in sync.