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.
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 library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.