簡體   English   中英

JavaFX將字符串正確綁定到TreeCell Factory

[英]JavaFX Binding String to TreeCell Factory Properly

我遇到一個問題,當我展開和展開樹單元時。 我綁定的文本永遠不會消失。.這可能是什么原因?

這就是我得到的 在此處輸入圖片說明

這是我的代碼

        treeView.setCellFactory(t -> {

                return new TreeCell<Entry>() {

                    @Override
                    protected void updateItem(Entry entry, boolean empty) {
                        super.updateItem(entry, empty);

                        if (entry != null) {

                            if (empty) {
                                setText(null);
                                setGraphic(null);
                            } else {

                                if (entry.getImage() != null) {

                                    Image image = FXUtils.toFXImage(entry.getImage());

                                    setGraphic(new ImageView(image));

                                } else {
                                    setGraphic(null);
                                }

                                textProperty().bind(entry.nameProperty());

                            }


                        }

                    }

                };

            });

如果單元格為空,則該項也為null 但是,僅當if (entry != null)時才執行所有邏輯,即,如果單元格為空或item為null ,則永遠不會執行。 此外,即使您修復了此錯誤,此操作也將失敗,因為您從未取消綁定textProperty ,但是可以為空單元格設置它。 將您的updateItem方法更改為如下所示:

@Override
protected void updateItem(Entry entry, boolean empty) {
    super.updateItem(entry, empty);

    textProperty().unbind();

    if (empty || entry == null) {
        setText(null);
        setGraphic(null);
    } else {
        if (entry.getImage() != null) {
            Image image = FXUtils.toFXImage(entry.getImage());
            setGraphic(new ImageView(image));
        } else {
            setGraphic(null);
        }

        textProperty().bind(entry.nameProperty());
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM