【发布时间】:2015-09-16 12:47:15
【问题描述】:
更新:这个问题移到TableColumn should only show one specific value of a complex data type,因为它更具体
我想用包含name、id 和List<Person> 的复杂数据类型Person 填充一个表。目前,我得到一个表格,其中包含正确的name、id 和第三列,其中包含其他Persons 的全部信息,但它应该只显示Personss 的名称。
有什么方法可以让我的第三列只显示 Person.getName() 值?
欢迎关键字,解决方案!非常感谢!
编辑:代码
Person.class
public class Person {
String id;
String name;
List<Person> friends;
public String getId() {...}
public String getName() {....}
public List<Person> getFriends {...}
}
TableView.class
public TableView<Person> createTable() {
TableColumn<Person, String> firstCol = new TableColumn<>("ID");
TableColumn<Person, String> secondCol = new TableColumn<>("Name");
TableColumn<Person, List<Person> thirdCol = new TableColumn<>("Friends");
PropertyValueFactory<Person, String> firstColFactory = new PropertyValueFactory<>(
"id");
PropertyValueFactory<Person, String> secondColFactory = new PropertyValueFactory<>(
"name");
PropertyValueFactory<Person, List<Person>> thirdColFactory = new PropertyValueFactory<>(
"friends");
firstCol.setCellValueFactory(firstColFactory);
secondCol.setCellValueFactory(secondColFactory);
thirdCol.setCellValueFactory(thirdColFactory);
myTableView.getColumns().addAll(firstCol, secondCol, thirdCol);
}
因此,如果我填写表格id 和name-colums 包含正确的名称,并且第三列(带有List<Person>)显示[Person [id1, John, ...]...]]。
现在我想问一下是否有可能第三列只显示id或name而不处理数据?
【问题讨论】:
-
你能贴一些代码吗,例如您的
Person班级以及您是如何设置表格和列的? -
也许这可以帮助你:stackoverflow.com/a/17072303/4170073
标签: javafx javafx-8 tablecolumn