Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TreeView with icons did not display correctly after expansion (JavaFX 8)

user7559371Mar 29 2014 — edited Apr 17 2014

I have a TreeView with customized CellFactory and TreeCell classes, which was implemented in JavaFX 2.2 and working perfectly. However, after I upgraded to JavaFX 8. This tree view is completely messed up. If I expand a nodes, there will be duplicated tree nodes shown in the tree. Here is a simplified version of source code. If do not use setGraphic() method in TreeCell class, it works correctly.

package test;

import javafx.application.Application;

import javafx.scene.Scene;

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.layout.BorderPane;

import javafx.stage.Stage;

import javafx.util.Callback;

public class TreeViewTest extends Application {

    private static final Image image = new Image(TreeViewTest.class.getResourceAsStream("icon_datasource.png"));

   

    public TreeViewTest() {

    }

    @Override

    public void start(Stage primaryStage) {

        BorderPane rootPane = new BorderPane();

        TreeItem<String> root = new TreeItem<String>("Root");

       

        for (int i = 0; i < 5; i++) {

            TreeItem<String> ds = new TreeItem<String>("Datasource " + i);

           

            for (int t = 0; t < 5; t++) {

        TreeItem<String> tb = new TreeItem<String>("Table " + t);

        ds.getChildren().add(tb);

            }

           

            root.getChildren().add(ds);

        }

       

        TreeView<String> tree = new TreeView<String>();

        tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {

    

     public TreeCell<String> call(TreeView<String> param) {

  return new TreeCell<String>() {

     @Override

     protected void updateItem(String item, boolean empty) {

         super.updateItem(item, empty);

        

         if (!empty) {

             ImageView imageView = new ImageView(image);

             setText(item);

             setGraphic(imageView);

         }

     }

  };

     }

  });

        tree.setRoot(root);

        tree.setShowRoot(false);

       

        rootPane.setLeft(tree);

       

        Scene scene = new Scene(rootPane, 1024, 800);

        primaryStage.setTitle("Test");

        primaryStage.setScene(scene);

        primaryStage.show();

    }

   

    public static void main(String[] args) {

        launch(args);

    }

}

        primaryStage.setTitle("Test");

        primaryStage.setScene(scene);

        primaryStage.show();

    }

   

    public static void main(String[] args) {

        launch(args);

    }

}

Thanks for any help.

This post has been answered by James_D on Mar 29 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2014
Added on Mar 29 2014
3 comments
3,723 views