简体   繁体   中英

JavaFX editable TableCell for Double property

How to set up converter for TableCell to be able to edit Double property?

<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? So far this looks really confusing.

There's no way to do this in FXML, as far as I can see.

The cellFactory you are providing to the TableColumn is a factory: its type is 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.

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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