在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(鼠标事件事件) 根据需要。

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

附录: JTextComponent JTextArea 的父级通过两种方法提供位置信息: 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.