JavaFX参数化可编辑的tableview [英] 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?
推荐答案
参数化<$ c总是更好$ c> TableView 和 TableColumn
。
您链接的代码有点遗留,因为它使用原始类型而不使用辅助单元类,例如 TextFieldTableCell
。
The code you linked is somewhat "legacy" as it uses raw types and doesn't utilize the helper cell classes, such as TextFieldTableCell
.
TextFieldTableCell
提供静态 forTableColumn(...)
方法,它接受 StringConverter< T>
并返回 Callback
,可以用作 cellFactory
表示 TableColumn< S,T>
。 StringConverter< T>
只提供了将 String
转换为 TextField的方法
进入 T
,以及将 T
转换为 String
将显示在单元格中。
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.
为数字类型提供标准的 StringConverter
,例如 IntegerStringConverter
和 DoubleStringConverter
。
There are standard StringConverter
s 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()));
这篇关于JavaFX参数化可编辑的tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!