JavaFX-获取cellFactory()的整行项目?

时间:2016-02-26 21:22:47

标签: java javafx tableview

说我有一个给定的TableView<Person>。我有一个LocalDate类型的列,其中包含cellFactory()集。

birthdayColumn.setCellFactory(column -> {
    return new TableCell<Person, LocalDate>() {
        @Override
        protected void updateItem(LocalDate item, boolean empty) {
            super.updateItem(item, empty);

            //Person person = ... I want to get entire Person, not just LocalDate item

            if (item == null || empty) {
                setText(null);
                setStyle("");
            } else {
                // Format date.
                setText(myDateFormatter.format(item));

                // Style all dates in March with a different color.
                if (item.getMonth() == Month.MARCH) {
                    setTextFill(Color.CHOCOLATE);
                    setStyle("-fx-background-color: yellow");
                } else {
                    setTextFill(Color.BLACK);
                    setStyle("");
                }
            }
        }
    };
});

有没有办法可以在Person方法中访问该记录的整个updateItem()?我想使用Person的其他属性来有条件地格式化单元格...

1 个答案:

答案 0 :(得分:2)

TableCell可以访问项目列表中的TableView及其索引,因此您可以这样做:

Person person = getTableView().getItems().get(getIndex());
相关问题