awyea
awyea

Reputation: 37

JavaFX parameterized editable tableview

I'm trying to add an editable table to my project, and I found this code that gives an outline to what I'm trying to do. However, it is not parameterized, which is what seems to make it work (My code keeps giving me type errors). Is there a way to parameterize this or are raw types ok in this situation?

Upvotes: 0

Views: 775

Answers (1)

James_D
James_D

Reputation: 209339

It's always better to parameterize the TableView and the TableColumns.

The code you linked is somewhat "legacy" as it uses raw types and doesn't utilize the helper cell classes, such as TextFieldTableCell.

TextFieldTableCell provides a static forTableColumn(...) method that takes a StringConverter<T> and returns a Callback which can be used as a cellFactory for a TableColumn<S,T>. The StringConverter<T> just provides methods for converting a String entered into the TextField into a T, and for converting a T into a String to be displayed in the cell.

There are standard StringConverters provided for numeric types, such as IntegerStringConverter and DoubleStringConverter.

Try something along the lines of:

TableView<MyDataType> table = new TableView<>();

TableColumn<MyDataType, Integer> intColumn = new TableColumn<>("Int Column");
intColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));

TableColumn<MyDataType, Double> doubleColumn = new TableColumn<>("Double Column");
doubleColumn.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));

Upvotes: 1

Related Questions