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

Do you need to process or display PDF files?

Find out why you should be using IDRSolutions software

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 software (Photoshop, Fireworks, GIMP, etc), PDF layers work exactly the same. There are defined layers that 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.

swing_all_enabled
All the layers enabled
swing_fore_gone
One Layer Disabled

As you can see on the sidebar, there are 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:

fx_standard

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:

layersTree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());

checkbox

So now we’re faced with the opposite problem, checkboxes 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:

bothfx

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.



Our software libraries allow you to

Convert PDF files to HTML
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
Read and write HEIC and other Image formats in Java
Simon Lissack Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

How to insert an image into a PDF

Recently, we released JPedal 2023.07 which contains the ability to insert images into PDF files. All you need is a copy of JPedal, a...
Jacob Collins
18 sec read

One Reply to “Mixed TreeView Nodes in JavaFX”

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

    return new TreeCell(){

    @Override
    public void updateItem(String item, boolean empty){

    super.updateItem(item, empty);

    if(empty){
    setGraphic(null);
    setText(null);
    } 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.

Comments are closed.