Simon Lissack Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

Mixed TreeView Nodes in JavaFX

1 min read

Recently for our JavaFX PDF Viewer I have been working on implementing the Layers panel. If you’re familiar with how layers work in various image manipulation softwares (Photoshop, Fireworks, GIMP, etc), PDF layers work exactly the same. There are defined layers which overlay one another and can be independently added/removed without affecting the other layers. The purpose of the panel is to allow the user to toggle the layers on and off. Below is an example of layers and the layers panel in our Swing PDF Viewer.

All the layers enabled
One Layer Disabled

As you can see on the sidebar, there are a two different types of cells we’re using to display items on the tree, DefaultMutableTreeNode and CheckNode (which is a custom Node implementation we use). In Swing, these two nodes can appear in the tree without any custom code. In JavaFX though, using both a TreeItem and a CheckBoxTreeItem in a standard TreeView gives us this:


To get around this, we need to use a Cell Factory to specify which type of cell we want to use. If we only wanted to use CheckBoxes, we could use the specific Cell Factory for CheckBoxTreeItems. Using the following code on the TreeView:



So now we’re faced with the opposite problem, check boxes but no normal tree items. However, we can get around this by implementing our own custom Cell Factory callback which returns a custom TreeCell. The custom TreeCell extends CheckBoxTreeCell and overrides the updateItem() method, in which we determine what type of Cell it should be.

Using this gives us the result we wanted:


In this example I have chosen to treat the Cells as all being a type of CheckBoxTreeCell. In this case it saves a lot of work as the majority of the TreeItems are CheckBoxTreeItems, all I need to do is disable the graphic to get the desired appearance.

An alternate approach would be to use a TreeCell in the cell factory and manually set up the checkboxes with bi-directional bindings. This approach is a lot more flexible as it allows for you to account for multiple types of TreeItems.

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Simon Lissack Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

One Reply to “Mixed TreeView Nodes in JavaFX”

  1. There’s another way to do this which I think is better:

    return new TreeCell(){

    public void updateItem(String item, boolean empty){

    super.updateItem(item, empty);

    } else {
    if(item instanceof MyLayerObject)
    setGraphic(new CheckBox());
    else if(item instanceof MyTextObject)
    setGraphic(new TextField());

    Using this you can easily construct any number of GUI elements for each TreeView row. The MyLayerObject and MyTextObject are just dummy classes used to denote the different tree view types. You would add them to your tree by instantiating each as appropriate and passing into a TreeItem object to pass to the TreeView.

Leave a Reply

Your email address will not be published. Required fields are marked *