3

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!

akrolsmir
  • 312
  • 1
  • 4
  • 14

3 Answers3

3

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.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
StanislavL
  • 56,971
  • 9
  • 68
  • 98