So I've got a TreeView working fine, then I tried to add an icon to each of the items using either the ctor: TreeItem<>(value, graphic) or the item.setGraphic(graphic) call. The icons show up on some of the items but not others, without a detectable pattern. Also, the icons sometimes appear when I collapse an item then disappear when I expand it.
I tried using a pretty large icon file, and now all of my items are spread out like they have left space for the icon, but only some of them show it.
Turns out this is happening only when I use a single ImageView instance for every TreeItem. I finally noticed that the pattern is that the last visible TreeItem to which the icon was attached is the only one that displays the icon, so it changes as I scroll or expand/collapse. Very weird.
When I changed the code to create a new ImageView instance for each TreeItem (while still using a single Image instance passed to each ImageView ctor), all the icons appear and work fine.
I'll put in a bug report at the link posted above.
This is not a bug then. One of the fundamental principles of the JavaFX scenegraph is that a node (in this case an ImageView) can only be in one place in a scenegraph at once. Your approach of creating a new ImageView node (based on a single Image) is the right way to go.
Ok, but there sure seems like there's a bug in there somewhere when all the TreeItems leave room for the icon but only one displays it. I'd say that behavior is at least surprising (and confusing). Might be better to throw an error when a node is used in more than one place, IMHO.