This discussion is archived
5 Replies Latest reply: May 22, 2013 2:49 AM by jsmith RSS

Problems customizing a tree view with a CheckBoxTreeCell

se*328623*78 Newbie
Currently Being Moderated
I have built a tree view using a CheckBoxTreeCell.
I would like to customize its presentation so a (clickable) image will appear to the left of the label. I tried to customize its presentation with the following custom tree cell:
private class MyCell extends CheckBoxTreeCell<MyTree>  {
          @Override 
          public void updateItem(MyTree node, boolean empty) {
               super.updateItem(node, empty);
               if(node == null) {
                    setText("");
               } else {
                    //setText(node.getName()); //this works fine
                    Label label = new Label(node.getName());
                    HBox hBox = new HBox();
                                ImageView imageView = ...
                    hBox.getChildren().add(imageView, label);
                    setGraphic(hBox); //this does not work
               }
          }
}
But this breaks the correct presentation of the check box tree (among other things, the check boxes are not there anymore).
Which is the correct way to customize the presentation of an item (e.g., adding a clickable image to it) in this scenario ?

Finally, what if I would like to add not just an image, but a context menu to items in this tree ?

Edited by: se**** on May 21, 2013 9:47 AM
  • 1. Re: Problems customizing a tree view with a CheckBoxTreeCell
    jsmith Guru
    Currently Being Moderated
    Which is the correct way to customize the presentation of an item (e.g., adding a clickable image to it) in this scenario ?
    Use CSS.
    Finally, what if I would like to add not just an image, but a context menu to items in this tree ?
    See how a TreeView item is made to react to a mouse click as in this example, and on click pop up context menu rather than updating label text:
    http://stackoverflow.com/questions/16658856/java-fx-2-treeview-model-reference

    Might be easier to start with subclassing a standard TreeCell rather than a CheckBoxTreeCell, otherwise review the source code for CheckBoxTreeCell and TreeCell, then make sure that when you override updateItem you aren't doing anything which would mess up the standard functioning of the CheckBoxTreeCell:
    http://hg.openjdk.java.net/openjfx/8/master/rt/file/a7934812ce83/javafx-ui-controls/src/javafx/scene/control/cell/CheckBoxTreeCell.java
    http://hg.openjdk.java.net/openjfx/8/master/rt/file/a7934812ce83/javafx-ui-controls/src/javafx/scene/control/TreeCell.java
  • 2. Re: Problems customizing a tree view with a CheckBoxTreeCell
    se*328623*78 Newbie
    Currently Being Moderated
    Thanks for answering, I will take a look to the internals of the CheckBoxTreeCell to see how can I change the default layout of the item without breaking the check box.
    But I have the feeling something in that class is wrong if there is not a straighforward mechanism for customizing the item presentation. It does not seem like an exotic requirement, isn't it ?
  • 3. Re: Problems customizing a tree view with a CheckBoxTreeCell
    jsmith Guru
    Currently Being Moderated
    I have the feeling something in that class is wrong
    You should check the issue tracker:
    https://javafx-jira.kenai.com/browse/RT-24666 "Public extensions of TreeCell do not render their item's graphic attribute"
    https://javafx-jira.kenai.com/browse/RT-30372 "CheckBoxTreeItem: extra checkbox on Treeview"
    https://javafx-jira.kenai.com/browse/RT-30388 "HelloTreeView: checked TreeView aligns children left"
  • 4. Re: Problems customizing a tree view with a CheckBoxTreeCell
    se*328623*78 Newbie
    Currently Being Moderated
    Do you know if the status of "fixed" in the first link means that "setGraphic(...)" should work in JavaFX 8 ?
  • 5. Re: Problems customizing a tree view with a CheckBoxTreeCell
    jsmith Guru
    Currently Being Moderated
    Do you know if the status of "fixed" in the first link means that "setGraphic(...)" should work in JavaFX 8 ?
    I tried the test code from RT-24666 link and it worked as expected.

    I changed the TextFieldTreeCell in the test code to CheckBoxTreeCell and it also rendered the graphic for the CheckBoxTreeCell when it was set, however it did still have the issue RT-30372, so that particular class is still buggy.

    Tested on OS X 10.8, Java 8b89

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points