我有一个表,它有一个带复选框的列.
在按钮上单击,我想找出哪些复选框被选中,哪些不是.到目前为止,我设法在表格中创建复选框.代码如下.
public class TTEs implements Initializable { @FXML private TableViewtableReport; @FXML private TableColumn name; @FXML private TableColumn 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 data = FXCollections.observableArrayList(); data.add(new TestObject("Test 1", true)); data.add(new TestObject("Test 2", false)); tableReport.setItems(data); name.setCellValueFactory(new PropertyValueFactory ("name")); checkbox.setCellValueFactory(new PropertyValueFactory ("checked")); checkbox.setCellFactory(new Callback , TableCell >() { public TableCell call(TableColumn p) { return new CheckBoxTableCell (); } }); } //CheckBoxTableCell for creating a CheckBox in a table cell public static class CheckBoxTableCell extends TableCell{ private final CheckBox checkBox; private ObservableValueov; 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); } } } } }
当我调试时,我发现:
ov = getTableColumn().getCellObservableValue(getIndex()); if (ov instanceof BooleanProperty) { checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov); }
在上面的条件中,它永远不会进入if
语句,这意味着它ov
不是一个实例BooleanProperty
.但是当我打印出来的时候ov
,
System.out.println(ov.getClass().getName());
它打印为
javafx.beans.property.ReadOnlyObjectWrapper
ReadOnlyObjectWrapper
是一个子类BooleanProperty
,为什么instanceof
检查不起作用?
在Java 8上测试过.
只有4个简单的东西.
1)制作CheckBoxCellFactory类.放在你的项目中的某个地方.
public class CheckBoxCellFactory implements Callback { @Override public TableCell call(Object param) { CheckBoxTableCell<Person,Boolean> checkBoxCell = new CheckBoxTableCell(); return checkBoxCell; } }
2)你的模特课.比如人.
public static class Person { private SimpleBooleanProperty checked = new SimpleBooleanProperty(false); // other columns here public SimpleBooleanProperty checkedProperty() { return this.checked; } public java.lang.Boolean getChecked() { return this.checkedProperty().get(); } public void setChecked(final java.lang.Boolean checked) { this.checkedProperty().set(checked); } // getter/setter for other columns }
3)在fxml文件中进行了修改.TableView - > TableColumn的部分将如下所示:
<TableColumn fx:id="checkBoxTableColumn" max min pref resizable="false" sortable="false"> <cellValueFactory><PropertyValueFactory property="checked" /></cellValueFactory> <cellFactory><partarch.fx.CheckBoxCellFactory /></cellFactory> </TableColumn>
4)如果您想使您的复选框可编辑
4.1在Scene Builder中打开fxml.
4.2选择checkBox列,然后在"属性"窗格中选中"可编辑"属性.
4.3选择包含复选框的TableView并执行相同操作(在"属性"窗格中选中"可编辑"属性).
在SF上的二进制文件和GitHub上的源代码中实现
循环TableView
检查绑定到每个的布尔值的数据模型CheckBox
.
@FXML public void getValues(){ ObservableList<TestObject> data = tableReport.getItems(); for (TestObject item : data){ //check the boolean value of each item to determine checkbox state } }