Mixed TreeView Nodes in JavaFX

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.

swing_all_enabled

All the layers enabled

swing_fore_gone

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, DefaultMutableTreeNodeand 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, 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:

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.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter,FacebookandGoogle+) or theBlog RSS.

Related Posts:

The following two tabs change content below.
Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.
Simon

About Simon Lissack

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

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>