I'm trying to add tooltips for the nodes of a Tree in GWT. As such, I'd like to add a mouseover listener for the nodes of a tree rather than on the tree itself.
The Treelistener interface seems to be what I want but this is now deprecated in lieu of the handler system. I don't quite understand how to get mouseover behaviour on the cell as I only seem to be able to add a MouseOverHandler to the tree itself.
Any help would be appreciated, thank you.
An alternative way that I settled upon is to make use of CSS.
With accompanying css:
Of course, you can change the style, add fades etc as you like.
Less javas/javascript and more css.
I'm going to stick my neck out a bit here since I haven't actually used a
Tree
in GWT yet, but I see that theTreeItem
class is a subclass ofUIObject
. AnyUIObject
can have itssetTitle()
method called. Under the hood, this method sets the standard HTML title attribute to be whatever string you pass intosetTitle()
.This should give you the tooltip behavior you seek. As an added bonus, the browser does all of the mouse event handling for you:
Edit: Now let's imagine that you don't want to use the browser's built-in tool-tip mechanism described above, and that you would like to handle the mouse events yourself.
TreeItem
might look, on the surface, as a non-starter. After all, it inherits directly fromUIObject
and not fromWidget
. (The key difference that aWidget
adds toUIObject
is, after all, the ability to handle events. So one would think that we cannot add handlers to the TreeItem!)While this is strictly true, notice that
TreeItem
gives us the following constructor:When we make each instance, then, we can pass a real
Widget
into it (such as aLabel
, perhaps, or maybe your ownclass MyWidget extends Composite
) and we can add event handlers directly to that:Note that there may be other ways to accomplish this that are less expensive. If you have an enormous tree, then creating a
Widget
for each node can get expensive. Then you might want to explore a more sophisticated way of dealing with your mouse events, perhaps by having one handler that checks to see which element it is in.A TreeItem can contains a Widget object. So add a MouseOverHandler and a MouseOutHandler on a widget (i.e. a Label) and put the widget inside the TreeItem to add :
An other solution can be to use GwtQuery. GwtQuery allows to bind event handler to any DOM element :
Julien