0

I am trying to display a list of users on a table. I write the following code in order to complete the table, however the Boolean values are not displayed on checkboxes (they are always populated as empty / false when there are actually several that are true). As a test I an just adding a single object that I am creating "manually" Below is the code:

TableView<User> objTable = new TableView<User>();
objTable.setEditable(true);
ObservableList<User> objList = FXCollections.observableArrayList(new User("User 1", true);
TableColumn objColumnName = new TableColumn<User, String>("Column Name");
TableColumn objColumnActive = new TableColumn<User, Boolean>("Active");
objColumnName.setCellValueFactory(new PropertyValueFactory<User, String>("DisplayName"));
objColumnActive.setCellValueFactory(new PropertyValueFactory<UserRequestVO, Boolean>("Active"));
objTable.getColumns().addAll(objColumn);
objTable.setItems(objList);

User Class

public class user
{
     private String strFirstName;
     private Boolean bolActive;
     public Boolean getActive()
     {
          return this.bolActive
     }
}

I also try renaming getActive function as isActive, but there were no changes

Cœur
  • 37,241
  • 25
  • 195
  • 267
delucaezequiel
  • 483
  • 2
  • 9
  • 26
  • 1
    Where is your code for displaying the checkboxes? Surely there is a [cell factory](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableColumn.html#cellFactoryProperty)? or a [CheckBoxTableCell](http://docs.oracle.com/javafx/2/api/javafx/scene/control/cell/CheckBoxTableCell.html)? You might want to review: [How to add CheckBox's to a TableView in JavaFX](http://stackoverflow.com/questions/7217625/how-to-add-checkboxs-to-a-tableview-in-javafx) and provide an [mcve] if you can't get it working. – jewelsea May 09 '17 at 23:01

1 Answers1

0

You should use properties,

In your user class, you would store your boolean as a SimpleBooleanProperty :

private SimpleBooleanProperty bolActive;

Instantiated like so : this.bolActive = new SimpleBooleanProperty(false); //Or true instead of false

Now create a getter for the property, the property value, and a setter for the property value :

public BooleanProperty bolActiveProperty(){
        return bolActive;
}

public final Boolean getBolActive() {
    return bolActive.get();
}

public final void setBolActive(Boolean bolActive) {
    this.bolActive.set(bolActive);
}

Now when you create your table columns, you do this :

objColumnActive.setCellValueFactory(cellData -> cellData.getValue().bolActiveProperty());

Or if you prefer old school java :

objColumnActive.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<User,Boolean>, ObservableValue<Boolean>>() {              

    @Override
    public ObservableValue<Boolean> call(CellDataFeatures<User, Boolean> cellData) {
        return cellData.getValue().bolActiveProperty();
    }
});

This also work I think, might be wrong though :

objColumnActive.setCellValueFactory(new PropertyValueFactory<User, Boolean>("bolActive"));

This will allow you to bind the User property to the column, so that any modification of the column will affect the value in the user.

Nice thing is you can listen to the value modification using
myProperty.addListener((obs, oldV, newV) -> { /* Your code */ });
Where obs is the value observed, oldV the old value, and newV the new value (obviously)

Does that help/work for you?

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36