Reacting to mouse events on a TreeCell. Is it supposed to be this awkward?

Scott Palmer swpalmer at
Tue Oct 8 12:37:59 PDT 2013

I'm investigating a memory leak and it seems that the culprit is event
listeners attached to TreeCells

The GC roots of my leaks are deep in the JavaFX window/event system

In a class extending TreeCell,  am calling methods on such as:


All of the event handlers in this case will have a reference to the
TreeCell, either via the implicit reference of the anonymous inner class,
or an explicit member

I do this in updateItem when the cell is not empty and has a non-null item.
If updateItem is called and the cell is empty or has a null item then I
clear the event handlers with:


The problem is that TreeView doesn't seem to reuse TreeCells very much.  It
mostly creates new ones.  This means that many TreeCells are disconnected
from the scene graph and "lost" while there is still an event handler
connected to it.

Am I doing something wrong?
This seemed like the correct way to deal with dragging and double clicking
on tree nodes.

The tutorial here:

only goes so far as to add a context menu. (My context menu needs to be
constructed dynamically.)  So I'm not sure if I'm "allowed" to connect
event handlers to TreeCells in this way, but I don't know what the
alternative is.

I suppose I would have to listen to something to ensure the TreeCell is
still part of the scene graph and disconnect the listeners when that
changes.  It seems a bit awkward.

(I'm using 7u40)

More information about the openjfx-dev mailing list