How do i set the grapic for a TextFieldTreeCell

89 views Asked by At

I made a treeview with normal treecells and a cellfactory to display my custom objects.

I wanned to be able to edit the object names in the treeview.

So i switched from a normal TreeCell to a TextFieldTreeCell so that i can edit the name of my objects in the tree.

But now the setGrapic methods in updateitem is not working. what am i doing wrong?

Also im not sure how to acces the custom object from the fromString methode for editing the object.

    treeView.setCellFactory(new Callback<TreeView<MenuItem>, TreeCell<MenuItem>>() {
        @Override
        public TreeCell<MenuItem> call(TreeView<MenuItem> menuItemTreeView) {
          //     MenuItemTreeCell menuItemTreeCell = new MenuItemTreeCell();
            TextFieldTreeCell<MenuItem> cell = new TextFieldTreeCell<MenuItem>(new StringConverter<MenuItem>() {
                @Override
                public String toString(MenuItem menuItem) {
                    if(menuItem==null){
                        return "";
                    }
                    return menuItem.getName();
                }
                @Override
                public MenuItem fromString(String s) {
                    // todo add code for changing name
                    return null;
                }
            })
            {
                @Override
                public void updateItem(MenuItem item, boolean empty) { // empty was b
                    super.updateItem(item, empty);

               //     setText((empty || item == null) ? "" : item.getName());
               //     checking if not null , and then what class to determ icon
                    if (item != null) {
                        switch (item.getClass().getSimpleName()) {
                            case "Group":
                                setGraphic(new ImageView(caseicon));
                                break;
                            case "Item":
                                setGraphic(new ImageView(basicicon));
                                break;
                            case "ClassItem":
                                setGraphic(new ImageView(classicon));
                                break;
                            case "MechanicItem":
                                setGraphic(new ImageView(mechanicicon));
                                break;
                        }
                    } else {
                        // no icon
                        setGraphic(null);
                    }

                    super.updateItem(item, empty);


                }
            };
0

There are 0 answers