I have a table and it has a column with checkboxes.
On a button click I want to find out which checkboxes are checked and which are not.
So far I managed to create checkboxes in a table.
The code is as follows.
public class TTEs implements Initializable {
@FXML
private TableView<TestObject> tableReport;
@FXML
private TableColumn<TestObject, String> name;
@FXML
private TableColumn<TestObject, Boolean> checkbox;
@FXML
public void getValues() {
//the method will get what check boxes are checked (this part is the problem)
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ObservableList<TestObject> data = FXCollections.observableArrayList();
data.add(new TestObject("Test 1", true));
data.add(new TestObject("Test 2", false));
tableReport.setItems(data);
name.setCellValueFactory(new PropertyValueFactory<TestObject, String>("name"));
checkbox.setCellValueFactory(new PropertyValueFactory<TestObject, Boolean>("checked"));
checkbox.setCellFactory(new Callback<TableColumn<TestObject, Boolean>,
TableCell<TestObject, Boolean>>() {
public TableCell<TestObject, Boolean> call(TableColumn<TestObject, Boolean> p) {
return new CheckBoxTableCell<TestObject, Boolean>();
}
});
}
//CheckBoxTableCell for creating a CheckBox in a table cell
public static class CheckBoxTableCell<S, T> extends TableCell<S, T> {
private final CheckBox checkBox;
private ObservableValue<T> ov;
public CheckBoxTableCell() {
this.checkBox = new CheckBox();
this.checkBox.setAlignment(Pos.CENTER);
setAlignment(Pos.CENTER);
setGraphic(checkBox);
}
@Override public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setGraphic(checkBox);
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);
}
ov = getTableColumn().getCellObservableValue(getIndex());
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
}
}
}
}
}
When I debug, I find that:
ov = getTableColumn().getCellObservableValue(getIndex());
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
}
In the above condition, it never goes inside the if
statement, meaning that the ov
is not an instance of BooleanProperty
. But when I print the class of ov
,
System.out.println(ov.getClass().getName());
it prints as
javafx.beans.property.ReadOnlyObjectWrapper
ReadOnlyObjectWrapper
is a subclass of BooleanProperty
, so why is the instanceof
check not working?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…