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

r3r*_*r57 0 javafx tablecolumn javafx-8

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

我想填充一个Person包含复杂数据类型的表name,id并且a List<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 {...}
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

因此,如果我填写表格id,name-colums包含正确的名称,第三列(with List<Person>)显示[Person [id1, John, ...]...]].现在我想问一下,第三列是否只显示idname不显示数据?

Ulu*_*Biy 5

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

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 );
});
Run Code Online (Sandbox Code Playgroud)

  • 使用`collect(Collectors.joining(", "))`而不是`reduce(...)`,这会更高效(循环中没有字符串连接...)。 (2认同)

Jam*_*s_D 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(", ")));
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

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

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);
            }
        }
    };
});
Run Code Online (Sandbox Code Playgroud)