Binding bug fixed in JavaFX TableView

I am new to JavaFX, so please bear with me. I am trying to have a TableView where some of the columns will be checkboxes. My intention is to bind them to logical properties in the model object. The model object has properties defined as SimpleBooleanProperty and have getter / setter and property methods. I checked that the table β€œsees” the model objects because I bind some logical columns as just text in the table, and, of course, the table displays β€œtrue” or β€œfalse” as expected. However, I cannot check the box to bind data in any direction. I have added some code examples below.

public class DataModel { private SimpleBooleanProperty prop1; private SimpleBooleanProperty prop2; public boolean getProp1() { return prop1.get(); } public setProp1(boolean value) { prop1.set(value); } public prop1() { return prop1; } ... } 

The logic of the user interface model:

 ... private ObjectProperty<ObservableList<DataModel>> listProperty; ... List<DataModel> list = new ArrayList<DataModel>(); ... add some DataModel objects to list final ObservableList<DataModel> obsList = FXCollections.observableArrayList(list); listProperty.set(obsList); 

UI logic:

 ... TableView table = new TableView<DataModel>(); table.setEditable(true); TableColumn<DataModel, String> boolAsStringCol = new TableColumn<DataModel, String>("Prop1"); boolAsStringCol.setCellValueFactory(new PropertyValueFactory<DataModel, String>("prop1")); TableColumn<DataModel, Boolean> boolAsCbxCol = new TableColumn<DataModel, Boolean>("Prop2"); boolAsCbxCol.setCellValueFactory(new PropertyValueFactory<DataModel, Boolean>("prop2")); boolAsCbxCol.setCellFactory(CheckBoxTableCell.forTableColumn(boolAsCbxCol)); boolAsCbxCol.setEditable(true); table.getColumns().add(boolAsStringCol); table.getColumns().add(boolAsCbxCol); ... 

I can toggle this checkbox, but it doesn't seem to bind the property to the checkbox. If I set a breakpoint, the setter is not called when I check the box or clear the check box. In addition, if I initialize the true property when creating the object, it will not be displayed, as noted when rendering the table.

Any suggestions are welcome. This seems to work, but it is not.

Thanks.

+4
source share
2 answers

you may need to add the following line to your code in the DataModel

  public SimpleBooleanProperty prop1Property() {return prop1;} public SimpleBooleanProperty prop2Property() {return prop2;} 
+2
source

Instead

 boolAsCbxCol.setCellFactory(CheckBoxTableCell.forTableColumn(boolAsCbxCol)); 

Use

 boolAsCbxCol.setCellFactory(CheckBoxTableCell.forTableColumn(boolAsCbxCol::getCellData)); 

Since the factory method you are using actually ignores its argument, and this is an error https://bugs.openjdk.java.net/browse/JDK-8186287

0
source

All Articles