GWT 树的工具提示:向节点添加鼠标悬停
我正在尝试为 GWT 中的树节点添加工具提示。因此,我想为树的节点而不是树本身添加一个鼠标悬停侦听器。
Treelistener 接口似乎是我想要的,但现在已弃用它来代替处理程序系统。我不太明白如何在单元格上获得鼠标悬停行为,因为我似乎只能将 MouseOverHandler 添加到树本身。
任何帮助将不胜感激,谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我要在这里冒险一下,因为我还没有在 GWT 中实际使用过 TreeItem,但我看到 TreeItem 类是
UIObject
。任何UIObject
都可以调用其setTitle()
方法。在幕后,此方法将标准 HTML 标题属性设置为您传递给setTitle()
的任何字符串。这应该会给你你想要的工具提示行为。作为一个额外的好处,浏览器会为您处理所有的鼠标事件:
编辑: 现在让我们假设您不想使用上述浏览器的内置工具提示机制,并且您想自己处理鼠标事件。
从表面上看,
TreeItem
可能看起来行不通。毕竟,它直接继承自UIObject
,而不是继承自Widget
。 ,Widget
添加到UIObject
的关键区别在于处理事件的能力。因此,人们会认为我们无法向 TreeItem 添加处理程序!)(毕竟 这是完全正确的,请注意
TreeItem
为我们提供了以下构造函数:当我们创建每个实例时,我们可以将一个真正的
Widget
传递给它(例如一个Widget
) >Label,也许,或者也许是您自己的class MyWidget extends Composite
),我们可以直接向其中添加事件处理程序:请注意,可能还有其他更便宜的方法来完成此任务。如果您有一棵巨大的树,那么为每个节点创建一个
Widget
的成本可能会很高。然后,您可能想要探索一种更复杂的方法来处理鼠标事件,也许可以通过一个处理程序来检查鼠标事件位于哪个元素中。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.TreeItem 可以包含 Widget 对象。因此,在小部件(即标签)上添加 MouseOverHandler 和 MouseOutHandler 并将小部件放入要添加的 TreeItem 中:
另一种解决方案是使用 GwtQuery。 GwtQuery 允许将事件处理程序绑定到任何 DOM 元素:
Julien
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
我选择的另一种方法是使用 CSS。
附带 css:
当然,您可以根据需要更改样式、添加淡入淡出等。
少一些 javas/javascript,多一些 css。
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.