Sadequer Rahman
Sadequer Rahman

Reputation: 133

How to set JavaFX CheckBox value in a TableView Column?

I am trying to get 'Active' value from 'Information' class and set it to TableView Column as Checkbox, so user can edit. I have following in my controller:

activeColumn.setCellValueFactory(new PropertyValueFactory<Information, Boolean>("Active"));
            final Callback<TableColumn<Information, Boolean>, TableCell<Information, Boolean>> cellFactory = CheckBoxTableCell.forTableColumn(activeColumn);
            activeColumn.setCellFactory(new Callback<TableColumn<Information, Boolean>, TableCell<Information, Boolean>>() {
                @Override
                public TableCell<Information, Boolean> call(TableColumn<Information, Boolean> column) {
                    TableCell<Information, Boolean> cell = cellFactory.call(column);
                    cell.setAlignment(Pos.CENTER);
                    return cell ;
                }
            });
            activeColumn.setCellFactory(cellFactory);
            activeColumn.setEditable(true);

Here is my 'Information' class where I'm getting Active value as true/false

public Information(Hashtable information) {
    :::

    String strActive = cvtStr(information.get("Active"));
            if (strActive.equals("1"))
                this.active = true;
            else if (strActive.equals("0"))
                this.active = false;
}
 public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

When I run it I'm not getting the Checkbox checked where 'Active' is true. Here is the screenshot:

enter image description here

Would any one tell me where am I doing wrong? Or is there any other ways to get this done?

Any help would be greatly appreciated

Upvotes: 0

Views: 5947

Answers (2)

Dan Rothman
Dan Rothman

Reputation: 169

I discovered that it is much simpler to make the property of the data object a type CheckBox rather than boolean. Then using the property in a tableview is MUCH easier and more straightforward.

Upvotes: -1

James_D
James_D

Reputation: 209358

Use JavaFX properties in your model class:

public class Information {

    private final BooleanProperty active = new SimpleBooleanProperty();

    public Information(Hashtable information) {

        // ...

        String strActive = cvtStr(information.get("Active"));
        if (strActive.equals("1"))
            setActive(true);
        else if (strActive.equals("0"))
            setActive(false);
    }

    public BooleanProperty activeProperty() {
        return active ;
    }

    public final boolean isActive() {
        return activeProperty().get();
    }

    public final void setActive(boolean active) {
        activeProperty().set(active);
    }

    // ...

}

Note that you have an error in your cell value factory (which you inexplicably set twice): it should be

activeColumn.setCellValueFactory(new PropertyValueFactory<Information, Boolean>("active"));

Better still would be

activeColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty());

Upvotes: 2

Related Questions