Ivan Yurov
Ivan Yurov

Reputation: 1618

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.

Upvotes: 3

Views: 2141

Answers (1)

James_D
James_D

Reputation: 209684

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()));
}

Upvotes: 6

Related Questions