我正在为我的 soap 服务编写一个 JavaFX 客户端,我的 fxml 页面必须包含一个完全可编辑的 TableView,它由 Product 类实体组成。我的表现在包含 2 个文本列和一个,其中包含双值。我想在单元格中添加一个带有 CheckBox 项目的选择列。使用 Ensemble 演示应用程序,我扩展了一个 Cell 类以使用 CheckBoxes:
public class CheckBoxCell<S, T> extends TableCell<S, T> {
private final CheckBox checkBox;
private ObservableValue<T> ov;
public CheckBoxCell() {
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);
}
}
}
@Override
public void startEdit() {
super.startEdit();
if (isEmpty()) {
return;
}
checkBox.setDisable(false);
checkBox.requestFocus();
}
@Override
public void cancelEdit() {
super.cancelEdit();
checkBox.setDisable(true);
}
}
然后在 fxml View Controller 类中,我为请求的 TableColumn 设置了一个 cellFactory:
private Callback<TableColumn, TableCell> createCheckBoxCellFactory() {
Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell> () {
@Override
public TableCell call(TableColumn p) {
return new CheckBoxCell();
}
};
return cellFactory;
}
...
products_table_remove.setCellFactory(createCheckBoxCellFactory());
我的问题是:
1) 如果我有,如何使用 PropertyValueFactory 用未选中的复选框填充此列
private final ObservableList <Boolean> productsToRemove= FXCollections.observableArrayList();
由 Boolean.FALSE 值组成,然后创建 View 。 (TableView 由没有 Boolean 属性的 Product 类组成(只有 3 个 String 和一个 Double 属性))?
2) 我可以使用 EventHandler 访问包含选定行的 Product 对象吗:
private void setProductDescColumnCellHandler() {
products_table_remove.setOnEditCommit(new EventHandler() {
@Override
public void handle(CellEditEvent t) {
...
我看到很多实体示例,它们有一个 boolean 字段。在我的例子中,我不想将 boolean 字段添加到 jax-ws 生成的类中。
最佳答案
1) 预定义类 javafx.scene.control.cell.CheckBoxTableCell可以用来代替你的。
2) 要向现有实例添加信息,我建议继承 + 委派,对于每个 data 实例,实例化一个 view 实例,该实例可用于提供 TableView :
class ProductV extends Product {
ProductV( Product product ) {
this.product = product;
}
final Product product;
final BooleanProperty delected = new SimpleBooleanProperty( false );
}
关于java - JavaFX 中的复选框单元格工厂 + Tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12571559/