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.
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:
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, 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:
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.
Are you a Developer working with PDF files?
Our developers guide contains a large number of technical posts to help you understand the PDF file Format.
Find out more about our software for Developers
|Convert PDF to HTML5 or SVG|
|Convert AcroForms and XFA to HTML5|
|Java PDF SDK for working with PDF files|