Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 914
Ivan Yurov Avatar asked Jan 24 '26 23:01

Ivan Yurov


1 Answers

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()));
}
like image 159
James_D Avatar answered Jan 26 '26 14:01

James_D