TornadoFX TreeView cell factory not being called

402 Views Asked by At

This is related to my previous question.

I have a TreeView in the center pane of a BorderPane layout. This TreeView is populated by the selection of an element from a list in the left pane. The center view looks like this:

class CenterView : View() {
    override val root = TreeView<IStoryItem>()
    init {
        with(root) {
            root = TreeItem(controller.storySet)

            setCellFactory {
                object : StoryEditorCell() {
                    init {
                        System.out.println("Creating StoryEditorCell")
                        onDragDetected = EventHandler {...}
                        onDragOver= EventHandler {...}
                        onDragEntered= EventHandler {...}
                        ...
                    }
                }
            }
            cellFormat { ... }
            populate { ... }
        }
    }
}

The setCellFactory function is being called but, for some reason, the init of the factory itself is never being called. So my Drag/Drop handlers are never set up and my TreeCell isn't of the correct type.

The cell format is correct and the TreeView is populated correctly so that part is working right. What else needs to be done to get the cell factory correct?

1

There are 1 best solutions below

6
On BEST ANSWER

Calling cellFormat actually creates a CellFactory, so it will effectively overwrite your configuration of a custom factory, hence your callbacks are never attached to a cell used in your TreeView.

If you manually call setCellFactory you must avoid cellFormat and instead override updateItem inside of the Cell to configure the text and graphic of the cell.

I looked at implementing custom support for DND so that you can combine cellFormat with DND without having to create a custom cell factory, but unfortunately I don't have time to begin this endeavor right now. If you feel that the current approach is cumbersome, please create an issue on GitHub and we'll get to this ASAP :)

In the mean time, remove the call to cellFormat and you should be good :) Remember the rules for overriding updateItem: Call super, clear out text/graphic, if !empty and you have a value, assign to text and/or graphic.

Also, don't do onDragDetected = EventHandler {...}, do setOnDragDetected {...} instead.