This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes how to activate html-links in a JEditor pane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to activate html-links in a JEditor pane" Watch "how to activate html-links in a JEditor pane" New topic
Author

how to activate html-links in a JEditor pane

Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Hi,

loading and displaying a html-file in a JEditorPane works fine (see code snippet provided),
except for the functionality of html links.

Do I have to provide this functionality separately in a programmatical way, and if yes, how is it accomplished?



SCJP, SCJD
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

The API for JEditorPane has a code snippet on using HyperlinkListener


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
thanks for the hint, that should do
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
common links work fine, but having a link referring to an email-adress like this:

results in the following exception:
java.net.UnknownServiceException: protocol doesn't support input

How can I achieve, that a JEditorPane invokes the default email program if clicking on that link?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Andy Jung wrote:results in the following exception:
java.net.UnknownServiceException: protocol doesn't support input

When? From your own code or from the API's code? Posting the stack trace may help.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
it comes from Sun's API. Complete stack trace is:



I used exactly the code snippet from the JEditorPane-API Maneesh suggested.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

The setPage method does not work for mailto URLs.

In the HyperlinkListener get the protocol of the URL. If that is "mailto" don't call setPage but use Desktop.mail(URL) instead.
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Rob Prime wrote:
In the HyperlinkListener get the protocol of the URL. If that is "mailto" don't call setPage but use Desktop.mail(URL) instead.


Thanks, that works ! A very nice new 1.6-feature I didn't know before.
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
is there any API-constant specifiying the protocol "mailto" for html-links, so I don't have to hard-code this (see following code-snippet)?


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to activate html-links in a JEditor pane
 
Similar Threads
JEditorPane problem
Open a Web Page inside a jDesktopPane
How to locate the help files
how to get row count of JEditorPane content
Display xml file in Pane