【问题标题】:Checkbox cell factory + Tableview on JavaFXJavaFX中的复选框单元工厂+ Tableview
【发布时间】:2014-01-27 00:25:58
【问题描述】:

我正在为我的 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 视图控制器类中,我为 requed 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 值组成,然后创建视图。 (TableView 由 Product 类组成,它没有布尔属性(只有 3 个 String 和一个 Double 属性))?

2) 我可以访问 Product 对象,其中包含使用 EventHandler 选择的行:

private void setProductDescColumnCellHandler() {
    products_table_remove.setOnEditCommit(new EventHandler() {
        @Override
        public void handle(CellEditEvent t) {
        ...

我看到了很多带有布尔字段的实体示例。就我而言,我不想将布尔字段添加到 jax-ws 生成的类中。

【问题讨论】:

    标签: java tableview javafx checkbox


    【解决方案1】:

    1) 可以使用预定义类 javafx.scene.control.cell.CheckBoxTableCell 代替您的。

    2) 要将信息添加到现有实例,我建议继承 + 委托,对于每个 data 实例,实例化一个可用于提供 TableView 的 view 实例:

    class ProductV extends Product {
    
       ProductV( Product product ) {
          this.product = product;
       }
    
       final Product         product;
       final BooleanProperty delected = new SimpleBooleanProperty( false );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-18
      • 2016-01-06
      • 2017-11-03
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 2013-03-31
      • 2019-04-21
      相关资源
      最近更新 更多