我有一个Kotlin数据类:
data class DataContainer(var descriptor: String = "",
var amount: Double = 0.0,
val composition: MutableMap<String, Double> = mutableMapOf(),
var date: LocalDate = now())
它的实例应作为条目添加到JavaFX TableView中。除组成之外,添加所有内容均符合TableColumn的标准
.setCellValueFactory(new PropertyValueFactory<>())
具有<>内的相关类型以及PropertyValueFactory()内的名称,例如:
tableColumnDC_date.setCellValueFactory(new PropertyValueFactory<DataContainer, LocalDate>("date"));
不幸的是,使用合成(Hash)Map,事情要复杂一些。仅使用上述标准过程,我就很难得到相关列的输出,例如
tableColumnDC_comp.setCellValueFactory(new PropertyValueFactory<DataContainer, HashMap<String, Double>>("composition"));
这就是为什么我要自定义setCellValueFactory方法。
我想产生一个定制的输出字符串,因为包含“ \ n”的字符串是在一行中整齐地组织输出的最简单方法。有什么建议?
您可以将所需的任何String值放在单元格中,方法是将其包装在setCellValueFactory中创建的ReadOnlyStringWrapper中。与PropertyValueFactory相比,它是一个更好的选择,因为它不使用反射,因此类型安全。
例如,下面的代码将为您提供地图中所有以换行符分隔的值。
tableColumnDC_comp.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().getMap().values().stream().collect(Collectors.joining("\n")));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句