JavaFX CheckBoxTreeItem: Graphic disappears if gra

2019-04-12 13:55发布

I have a bunch of CheckBoxTreeItem<MyClass> to build a hierarchy of items using a TreeView. I create a new object and add it to its parent like this:

CheckBoxTreeItem<MyClass> created = new CheckBoxTreeItem<MyClass>(
    myClassObj, new Label("label");

parent.getChildren().add(created); // parent is another CheckBoxTreeItem

Besides that, I use a CellFactory like this:

myTreeView.setCellFactory(CheckBoxTreeCell.<MyClass>forTreeView());

Problem: If I collapse / expand the the items in the GUI, the Label occasionally disappears, leaving a blank space where it should be rendered. So it is kind of there, but not visible. However, the text right to the Label (from the MyClass object) is shown. I observed the problem with my own classes extending CheckBoxTreeItem and CheckBoxTreeCell, but the problem seems to be more general, as it is reproducable with the default classes. If you have an idea, I'm thankful for any hints.

EDIT: Here is a MCVE - just expand and collapse a few of the checkboxes to observe the strange behaviour:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TreeViewSample3 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        //create the root element
        CheckBoxTreeItem<String> rootItem = 
            new CheckBoxTreeItem<String>("Root", new Label("Root"));

        rootItem.setExpanded(true); 
        rootItem.setIndependent(true);

        final TreeView<String> tree = new TreeView<String>(rootItem);  
        tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());  

        //put some checkboxes to the tree
        for (int i = 0; i < 3; i++) {
            final CheckBoxTreeItem<String> child = 
                new CheckBoxTreeItem<String>("2nd level", new Label("tier 2"));
            final CheckBoxTreeItem<String> grandChild = 
                new CheckBoxTreeItem<String>("3rd level", new Label("tier 3"));

            child.getChildren().add(grandChild);
            rootItem.getChildren().add(child);

        }

        StackPane root = new StackPane();
        root.getChildren().add(tree);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

EDIT 2: Screenshot of the error. It only occurs if you repeatedly expand/collapse items in the tree. It seems like the item at the bottom are somehow not correctly rendered. Note, the used version is 8.40.

enter image description here

0条回答
登录 后发表回答