Formatting JEditorPane's cursor, tooltips, links

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 18:05:01

问题


Right now, I'm using Java Swing to create a JEditorPane primarily for its ability to have hyperlinks. I've successfully been able to display links and have them execute behavior upon a click, but I'm running into a few problems with formatting.

  1. How can I set the cursor so that it normally is an arrow, but changes to a text cursor when hovering over text? (In essence, the behavior a cursor has within a web browser). I tried

    EditorPane.setCursor(new Cursor(Cursor.TEXT_CURSOR))
    

    but that made it a text cursor everywhere, even when not hovering over text. Right now, hovering over a link shows a pointer hand; I'd like to maintain that functionality as well.

  2. What is the best way to show tooltips or mouseover text when hovering over a link? I tried modifying the title attribute of the link but nothing showed up.

  3. I was trying to implement links to skip down to a subsection of the page, much like http://en.wikipedia.org/wiki/Xkcd#History would take you directly to the History subsection of Wikipedia's xkcd page. How can I do this?

An answer to any of these would be great (and multiple would be awesome xP). Thanks a lot for your help!


回答1:


As you said one can simply give answers to a single point as well, let me try one by one, here is the answer for your last Point 3

Just provide an id to your tag like this

<h1><a id = "top"></a>First Line</h1>

Now somewhere in the bottom of your page write this :

<p><a href = "#top">Return to TOP</a></p>

Clicking this link, you will reach the above area of the PAGE.




回答2:


Points 1 & 2 may be addressed using the approach mentioned here. In particular, the view/model conversion methods will let you condition setCursor() and getToolTipText(), respectively.




回答3:


You can get source from here http://java-sl.com/JEditorPaneStructureTool.html It shows how to obtain text view bounds. First you get caret position for current mouse poiunter using viewToModel() method. Then go down the Views tree achieving leaf view and calcualte it's bounds. See this http://java-sl.com/tip_view_rectangle.html

If your mouse pointer in the view's rectangle then your mouse over text.

You can check whether the text in caret position is link and show your tooltip.

Use this http://java-sl.com/tip_links_in_editable.html to see how to detect whether mouse is over link.

Point 3.rd is answered by @nIcE cOw



来源:https://stackoverflow.com/questions/10117286/formatting-jeditorpanes-cursor-tooltips-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!