Swing JTextArea 上的文本鼠标悬停弹出窗口?

问题描述:

是否有任何东西可以让您在 Swing JTextArea 中的单个单词或字母上显示一个小的文本弹出窗口(如工具提示)?(或具有类似功能的 JTextArea 替代品.)

Is there anything out there that allows you to show a small text popup window (like a tooltip) over individual words or letters in a Swing JTextArea? (Or a JTextArea alternative with similar functionality.)

我需要的应该表现得像一个工具提示,换句话说,只在鼠标悬停在单词上一两秒钟后才显示弹出文本,一旦鼠标移开它就会自动消失.当然,这里棘手的部分是我希望它在文本中的字符/单词级别,而不是组件级别......有什么建议吗?

What I need should behave like a tooltip, in other words only display the popup text after the mouse has hovered over the word for a second or two, and it would vanish automatically once the mouse moves away. Of course the tricky part here is that I want it at the character/word level within the text, not at the component level... any suggestions?

您可以覆盖 getToolTipText(Mouse Event event) 根据需要.

You can override getToolTipText(Mouse Event event) as needed.

附录:JTextComponentJTextArea 的父级通过两种方法提供位置信息:modelToView()viewToModel().后者应该让您将鼠标位置转换为文档偏移量.

Addendum: JTextComponent, the parent of JTextArea provides location information via two methods: modelToView() and viewToModel(). The latter should let you translate the mouse location into a document offset.