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

Scott Palmer swpalmer at gmail.com
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:

setOnContextMenuRequested(contextMenuRequestHandler);
setOnMouseClicked(mouseEventHandler);
setOnDragDetected(dragDetectedHandler);

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:

setOnContextMenuRequested(null);
setOnMouseClicked(null);
setOnDragDetected(null);

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:
http://docs.oracle.com/javafx/2/ui_controls/tree-view.htm#BABDEADA

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.


Scott
(I'm using 7u40)


More information about the openjfx-dev mailing list