1

Step to produce the bug

  1. Run this code
  2. Expand the root node, if not expanded
  3. Collapse the root node
  4. More folders (like subfolders) will appear at the end of the window

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class Main extends Application {

    Pane box;
    TreeView<String> treeView;

    @Override
    public void start(Stage stage) {

        TreeItem<String> root = new TreeItem<>("Root");
        traverse("a folder path", root);
        root.setExpanded(true);

        treeView = new TreeView<String>(root);
        treeView.setShowRoot(true);
        treeView.setEditable(true);
        treeView.setPrefSize(500, 800);

        treeView.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
            @Override
            public TreeCell<String> call(TreeView<String> paramP) {
                return new TreeCell<String>() {
                    @Override
                    protected void updateItem(String paramT, boolean paramBoolean) {
                        super.updateItem(paramT, paramBoolean);
                        if (!isEmpty()) {
                            setGraphic(new Label(paramT, (Node) importImage("image path")));
                            setOnMouseClicked(new EventHandler<MouseEvent>() {
                                @Override
                                public void handle(MouseEvent event) {

                                    P.print(paramT);
                                    P.print(treeView.getChildrenUnmodifiable().size());
                                }
                            });
                        }
                    }
                };
            }
        });

        box = new Pane();
        box.getChildren().add(treeView);
        box.setPrefSize(500, 800);

        Scene scene = new Scene(box, 500, 800);

        stage.setTitle("Tree View Sample");
        stage.setScene(scene);
        stage.show();
    }

    public void traverse(String path, TreeItem<String> root) {
        File file = new File(path);

        if (!file.isDirectory() || !Files.isReadable(Paths.get(path)))
            return;
        Arrays.asList(file.listFiles()).forEach(e -> {
            TreeItem<String> temp = new TreeItem<>(e.getName());
            root.getChildren().add(temp);
            traverse(e.getPath(), temp);
        });

    }

    public static ImageView importImage(String path) {
        ImageView image = new ImageView(new Image(path));
        image.setPreserveRatio(true);
        image.setFitHeight(20);

        return image;
    }

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

}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
M. Fahim
  • 21
  • 2
  • 1
    What is crashes? Is there a stack trace? If so, can you add it as text to the question formatted as code? – jewelsea Jul 11 '22 at 19:14
  • 2
    Note that "crash" has a pretty standard meaning in software development. Typically, it means there was a critical and catastrophic failure that caused the entire process to terminate. Your description does not match that kind of failure. It seems you're simply seeing visual "artifacts", and based on the code provided, that's caused by the improper implementation of `updateItem` in your custom cell (see link by James_D). – Slaw Jul 11 '22 at 20:54
  • Your example works if I follow the [API](https://openjfx.io/javadoc/17/javafx.controls/javafx/scene/control/Cell.html#updateItem(T,boolean)), as shown [here](https://stackoverflow.com/a/26822360/230513). If this is not a duplicate, please [edit] your question to include a [mre] that shows your revised approach. – trashgod Jul 11 '22 at 22:29
  • @jewelsea, thanks for the reply, there was not stack trace. – M. Fahim Jul 11 '22 at 22:57
  • 1
    Thanks @Slaw and trashgod, I think the is duplicated question. The answer you guys pointed out fixed the issue I had. – M. Fahim Jul 11 '22 at 22:58
  • Also consider `TreeView` and `TreeItem`. – trashgod Jul 12 '22 at 18:50

0 Answers0