在 jlist 中的项目悬停时显示框
我想当用户将鼠标悬停在列表中的项目上时显示包含信息的框。类似如下:
我该怎么做?这可以在 pidgin 或 Spark 等聊天应用程序中看到。
I want to show box that contains information when user hovers on an item in list. Something like follows:
How can I do this? This can be seen in chat app like pidgin or spark.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您正在寻找类似工具提示的功能。如果是这样,请考虑提供 ListCellRenderer 组件(例如 JLabel),并设置 JLabel 的工具提示。
EG 在工具提示中使用 HTML 渲染。
Perhaps you are after a tool tip like functionality. If so, look into providing a ListCellRenderer component such as a JLabel, and set the tool tip of the JLabel.
E.G. of using HTML rendering in a tool tip.
以下是我尝试实现它的方法:
将 MouseListener 和 MouseMotionListener 添加到您的 JList。当鼠标进入列表时,启动一个线程等待一定的延迟(半秒)。当鼠标移动或拖动时,重新开始等待延迟。当鼠标退出JList时,取消线程。还可以使用这些侦听器来跟踪鼠标位置。
一旦满足延迟(这应该意味着鼠标在整个延迟期间一直停留在列表上,没有移动),然后使用 SwingUtilities.invokeLater 显示信息框。您可以使用 JList 的 locationToIndex 来确定鼠标悬停在哪一行。
Here's how I would try to implement it :
Add a MouseListener and MouseMotionListener to your JList. When the mouser enters in the list, start a thread waiting for a certain delay (half a second). When the mouse is moved or dragged, restart waiting for the delay. When the mouse exits the JList, cancel the thread. Use these listeners to track the mouse position as well.
Once the delay has been met (which should thus mean that the mouse has stayed on the list, without moving, for the whole delay), then use SwingUtilities.invokeLater to show the information box. You can use JList's locationToIndex to determine which row the mouse is hovering on.