【发布时间】:2016-06-28 23:13:35
【问题描述】:
我在每一行都有带有复选框的表格视图,并且我有一个操作按钮。我的问题是,如何从 tableview 中选择复选框以在按下按钮时应用操作?
这就是我将复选框添加到表格视图的方式
public void addCeckBoxToTableView() {
/** define a simple boolean cell value for the action column so that the column will only be shown for non-empty rows. */
tcCb.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Object, Boolean>,
ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Object, Boolean> p) {
return new SimpleBooleanProperty(p.getValue() != null);
}
});
/** create a cell value factory with an add button for each row in the table. */
tcCb.setCellFactory(new Callback<TableColumn<Object, Boolean>, TableCell<Object, Boolean>>() {
@Override
public TableCell<Object, Boolean> call(TableColumn<Object, Boolean> p) {
return new CheckBoxCell();
}
});
}
private class CheckBoxCell extends TableCell<Object, Boolean> {
CheckBox checkBox = new CheckBox();
HBox hb = new HBox(checkBox);
/**
* places button in the row only if the row is not empty.
*/
@Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(hb);
} else {
setGraphic(null);
}
}
}
诚挚的。
【问题讨论】:
-
使用该设置,您不能。问题是:1. 当您的复选框被选中/取消选中时,您没有设置布尔属性的值,以及 2. 您没有保留对提供给
cellValueFactory的布尔属性的引用。使用标准的CheckBoxTableCell并查看stackoverflow.com/questions/28671132 或stackoverflow.com/questions/34940563(或其他,只需搜索...)
标签: intellij-idea javafx javafx-2 javafx-8