简体   繁体   English

JavaFX可编辑的TableCell for Double属性

[英]JavaFX editable TableCell for Double property

How to set up converter for TableCell to be able to edit Double property? 如何为TableCell设置转换器以能够编辑Double属性?

<TableColumn fx:id="priceCol" text="Price">
  <cellValueFactory>
    <PropertyValueFactory property="price" />
  </cellValueFactory>
  <cellFactory>
    <TextFieldTableCell fx:factory="forTableColumn">
      <converter>
      // ???
      </converter>
    </TextFieldTableCell>
  </cellFactory>
</TableColumn>

And where to find the documentation on how to set these kind of things with FXML? 在哪里可以找到有关如何使用FXML设置此类文档的文档? So far this looks really confusing. 到目前为止,这看起来确实令人困惑。

There's no way to do this in FXML, as far as I can see. 据我所知,在FXML中无法做到这一点。

The cellFactory you are providing to the TableColumn is a factory: its type is Callback<TableColumn<?,?>,TableCell<?,?>> . 您为TableColumn提供的cellFactory是一个工厂:其类型为Callback<TableColumn<?,?>,TableCell<?,?>> The object returned by the call to the static factory method TextFieldTableCell.forTableColumn() , which is what the FXMLLoader creates according to the element <TextFieldTableCell fx:factory="forTableColumn"> , is of this type. FXMLLoader根据元素<TextFieldTableCell fx:factory="forTableColumn">创建的静态工厂方法TextFieldTableCell.forTableColumn()的调用返回的对象是这种类型的。

The FXML code you provided tries to invoke setConverter(...) on the object returned by TextFieldTableCell.forTableColumn() , and of course Callback does not define a setConverter(...) method, so no matter what you include as a child tag of <converter> it is not going to work. 您提供的FXML代码尝试在TextFieldTableCell.forTableColumn()返回的对象上调用setConverter(...) ,当然, Callback不会定义setConverter(...)方法,因此无论您作为子对象包含什么<converter>标记将不起作用。

As far as I am aware, there is no way in FXML to provide a parameter to a factory method invoked via fx:factory (which is what I think you intend), so I think you have to do this in the controller. 据我所知,FXML中没有办法为通过fx:factory (我认为您想要的)调用的工厂方法提供参数,因此我认为您必须在控制器中执行此操作。

Of course, the latter is pretty easy. 当然,后者非常容易。 Just do 做就是了

<TableColumn fx:id="priceCol" text="Price">
  <cellValueFactory>
    <PropertyValueFactory property="price" />
  </cellValueFactory>
</TableColumn>

in FXML, and in your controller do 在FXML和您的控制器中

public void initialize() {
    priceCol.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM