JavaFx:如何正确触发 TableCell 中的 updateItem

JavaFx: How to properly fire updateItem in a TableCell

我必须实现大量自定义 TableCell,其行为依赖于模型的更改。我可以设法以某种方式获得预期的结果,但我认为在许多情况下这是一种变通方法,而不是一个非常好的解决方案。 我已经使用 bindings/listeners 来达到预期的结果,但我面临的问题是我可能会多次添加 listeners/bind 属性,这会造成内存泄漏。

这是我的意思的例子。

控制器:

public class Controller implements Initializable {

    @FXML private TableView<Model> table;
    @FXML private TableColumn<Model, String> column;
    @FXML private Button change;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        column.setCellValueFactory(data -> data.getValue().text);
        column.setCellFactory(cell -> new ColoredTextCell());

        Model apple = new Model("Apple", "#8db600");

        table.getItems().add(apple);
        table.getItems().add(new Model("Banana", "#ffe135"));

        change.setOnAction(event -> apple.color.setValue("#ff0800"));

    }

    @Getter
    private class Model {
        StringProperty text;
        StringProperty color;

        private Model(String text, String color) {
            this.text = new SimpleStringProperty(text);
            this.color = new SimpleStringProperty(color);
        }
    }

    private class ColoredTextCell extends TableCell<Model, String> {

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty || getTableRow() == null || getTableRow().getItem() == null) {
                setGraphic(null);
                return;
            }
            Model model = (Model) getTableRow().getItem();
            Text text = new Text(item);
            text.setFill(Color.web(model.getColor().getValue()));

            // This way I add the listener evey item updateItem is called.
            model.getColor().addListener((observable, oldValue, newValue) -> {
                if (newValue != null) {
                    text.setFill(Color.web(newValue));
                } else {
                    text.setFill(Color.BLACK);
                }
            });
            setGraphic(text);
        }
    }

}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="Whosebug.tabpane.Controller">
    <VBox>
        <Button fx:id="change" text="Change color"/>
        <TableView fx:id="table">
            <columns>
                <TableColumn fx:id="column" prefWidth="200"/>
            </columns>
        </TableView>
    </VBox>
</AnchorPane>

由于颜色 属性 不是由单元格直接观察到的,如果它发生变化,则不会调用 updateItem,所以我必须以某种方式听。 我需要在更改 color 后触发 updateItem 。这将导致对侦听器内容的单次调用。

有没有办法在同一个单元格中监听模型的另一个变化,或者以某种方式调用更新项,以便呈现变化。

我想你可以反过来做。

我会像这样创建颜色 属性:

    ObjectBinding<Paint> colorProperty = Bindings.createObjectBinding(()->{
        String color = model.getColor().get();
        return Paint.valueOf(color==null?"BLACK":color);
    } , model.getColor());

然后我会像这样绑定 属性:

text.fillProperty().bind(model.colorProperty);

如果你有:

会更简单
    SimpleObjectProperty<Paint> textColor = new SimpleObjectProperty<Paint>(Paint.valueOf("BLACK"));

然后在模型的 getter 和 setter 中更新 属性。

只要您记得在不再需要时将其删除,使用侦听器和绑定就不会造成任何问题。为了让它更安全,你应该使用弱监听器(绑定使用弱监听器)。当您想根据行项目的不同 属性 更改单元格文本的颜色时,我认为使用绑定会更容易。请注意,TableCell 继承自 Labeled,这意味着它有一个 textFill 属性;无需创建 Text 节点来更改文本的颜色。

这是一个例子:

import javafx.beans.binding.Bindings;
import javafx.scene.control.TableCell;
import javafx.scene.paint.Color;

public class ColoredTextCell extends TableCell<Model, String> {

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        /*
         * I was getting a NullPointerException without the "getTableRow() == null"
         * check. I find it strange that a TableCell's "updateItem" method would be
         * invoked before it was part of a TableRow... but the added null check seems
         * to solve the problem (at least when only having two items in the table and
         * no scrolling).
         */
        if (empty || item == null || getTableRow() == null) {
            setText(null);
            textFillProperty().unbind();
        } else {
            setText(item);

            Model rowItem = getTableRow().getItem();
            textFillProperty().bind(Bindings.createObjectBinding(
                    () -> Color.valueOf(rowItem.getColor()),
                    rowItem.colorProperty()
            ));
        }
    }

}

textFillProperty().unbind() 的调用将防止内存泄漏。并且当绑定 属性 时,先前的绑定(如果有)将被删除。如果你真的很偏执,你也可以在 bind(...) 之前调用 unbind()。如果你真的 真的 偏执狂那么你可以将 ObjectBinding 存储在一个字段中并在适当的时候调用 dispose() (甚至将其取消)。