[英]JavaFX: How do you display the contents of a String[] in a TableColumn?
I have a class for movies. 我有电影课。 The actors are saved in a String[]. 角色保存在String []中。 I created a GUI with the SceneBuilder. 我使用SceneBuilder创建了一个GUI。 I am setting the values of each TC like so: 我像这样设置每个TC的值:
tableDirector.setCellValueFactory(new PropertyValueFactory<>("Director"));
tableActors.setCellValueFactory(new PropertyValueFactory<>(("actors")));
That works for all the non-Collections stuff, but I cannot for the love of everything that's holy figure out how to put several Strings from a String[] in a single Column. 这适用于所有非Collections的东西,但是我不能为所有想找出如何将String []中的多个String放入单个列中的事物而热爱。 Am I missing something? 我想念什么吗?
Assuming all the code you haven't shown is correct, you can do 假设您未显示的所有代码都是正确的,则可以执行
tableActors.setCellFactory(tc -> new TableCell<Movie, String[]>() {
@Override
protected void updateItem(String[] actors, boolean empty) {
super.updateItem(actors, empty);
if (empty || actors == null) {
setText(null);
} else {
setText(String.join(", ", actors));
}
}
});
A more sophisticated cell factory might produce a cell that displayed the actors in a ListView
, or some similar option. 一个更复杂的单元工厂可能会产生一个在ListView
或类似选项中显示角色的单元。
Note that the cellFactory
is in addition to the cellValueFactory
you have already defined. 请注意, cellFactory
是您已经定义的cellValueFactory
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.