如何在表列上显示列表,列表项的字段很少 [英] How to show a list on table column, with few fields of list items

查看:242
本文介绍了如何在表列上显示列表,列表项的字段很少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 :此qustion已移至 TableColumn应该只显示复杂数据类型的一个特定值,因为它更具体

我想填充一个复杂数据类型 Person 的表,其中包含 name id 列表< Person> 。目前我得到一个表格,其中包含正确的名称 id 以及第三列包含其他<的全部信息 Person s但它应该只显示 Persons 的名称。
我的第三列是否有任何方式只显示 Person.getName()值?

I want to populate a table with a complex data type Person containing name, id and a List<Person>. At the moment I get a table with the correct name, id and a third column with the whole information of other Persons but it should only show the name of the Personss. Is there any way that my third column shows only the Person.getName() values?

关键词,欢迎解决!非常感谢!

Keywords, solutions welcome! Thank you very much!

编辑:代码

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 没有操作数据?

So if I fill the table id and name-colums contain the correct name and the third column (with List<Person>) shows [Person [id1, John, ...]...]]. And now I want to ask if theres a possibility that the third column only displays the id or name without manipulating the data?

推荐答案

作为Uluk解决方案的替代方案,请考虑在列上设置单元格工厂,而不是单元格价值工厂。它们之间的选择取决于您如何看待列与模型之间的关系( Person )。如果您认为名称列表是列显示的数据,那么Uluk的解决方案就是您的选择。如果您认为 Person 对象的列表是由其名称显示的数据,那么这将是更直观的选项:

As an alternative to Uluk's solution, consider setting the cell factory on the column, instead of the cell value factory. The choice between these depends on how you regard the relationship between the column and the model (Person). If you consider the list of names to be the data displayed by the column, then Uluk's solution is the way to go. If you consider the list of Person objects to be the data, which are displayed by their names, then this will be the more intuitive option:

TableColumn<Person, List<Person> thirdCol = new TableColumn<>("Friends");
PropertyValueFactory<Person, List<Person>> thirdColFactory = new PropertyValueFactory<>("friends");
thirdCol.setCellValueFactory(thirdColFactory);

thirdCol.setCellFactory(col -> new TableCell<Person, List<Person>>() {
    @Override
    public void updateItem(List<Person> friends, boolean empty) {
        super.updateItem(friends, empty);
        if (empty) {
            setText(null);
        } else {
            setText(friends.stream().map(Person::getName)
                .collect(Collectors.joining(", ")));
        }
    }
});

这样可以轻松更改列表在该列中的显示方式,例如:

This makes it easy to change the way the list is displayed in that column, e.g.:

thirdCol.setCellFactory( col -> {
    ListView<Person> listView = new ListView<>();
    listView.setCellFactory(lv -> new ListCell<Person>() {
        @Override
        public void updateItem(Person person, boolean empty) {
            super.updateItem(person, empty);
            if (empty) {
                setText(null);
            } else {
                setText(person.getName());
            }
        }
    });
    return new TableCell<Person, List<Person>>() {
        @Override
        public void updateItem(List<Person> friends, boolean empty) {
            super.updateItem(friends, empty);
            if (empty) {
                setGraphic(null);
            } else {
                listView.getItems().setAll(friends);
                setGraphic(listView);
            }
        }
    };
});

这篇关于如何在表列上显示列表,列表项的字段很少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆