如何在表列上显示列表,列表项的字段很少

时间:2015-06-29 21:45:21

标签: javafx javafx-8 tablecolumn

更新 :此qustion已移至TableColumn should only show one specific value of a complex data type,因为它更具体

我想填充包含Personnameid的复杂数据类型List<Person>的表格。目前,我得到的表格中包含正确的nameid和第三列,其中包含其他Person的全部信息,但它只应显示{{1}的名称}秒。 我的第三列是否有任何方式只显示Persons值?

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

编辑:代码

Person.class

Person.getName()

TableView.class

public class Person {
    String id;
    String name;
    List<Person> friends;

public String getId() {...}
public String getName() {....}
public List<Person> getFriends {...}
}

因此,如果我填写表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)显示List<Person>。 现在我想询问是否有可能第三列只显示[Person [id1, John, ...]...]]id而不操纵数据?

2 个答案:

答案 0 :(得分:5)

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

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(", ")));
        }
    }
});

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

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);
            }
        }
    };
});

答案 1 :(得分:4)

您可以使用长扩展版本定义单元格值工厂,您可以在其中控制要显示的字段:

thirdColFactory.setCellValueFactory(
  ( TableColumn.CellDataFeatures<Person, String> p ) ->
{
    List<Person> friends = p.getValue().getFriends();
    String val = friends
                 .stream()
                 .map( item -> item.getName() )
                 .reduce( "", ( acc, item ) -> acc + ", " + item );
    return new ReadOnlyStringWrapper( val );
});
相关问题