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

Jonathan Giles jonathan.giles at oracle.com
Tue Oct 8 12:48:34 PDT 2013


I note right at the end you say you're using 7u40. In the JavaFX 2.x
series of releases it is true that TreeView misbehaved a bit around cell
reuse. This should no longer be the case in JavaFX 8.0. In any case, the
best option is to file a bug report and discuss it further in there,
especially so if you can reproduce the issue in JavaFX 8.0.

One thing that sticks out: you don't say if you're using
WeakEventHandler or EventHandler instances. If you're using EventHandler
you could consider using WeakEventHandler. This may help to alleviate
some of the memory leak.

-- Jonathan

On 9/10/2013 8:37 a.m., Scott Palmer wrote:
> 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