X

Geertjan's Blog

  • June 26, 2006

Teaching JEditorPanes About Hyperlinks

Geertjan Wielenga
Product Manager
In Showing a Module's HTML File in the IDE's Browser, I made the mistake of thinking that I was describing how to open an HTML file in the IDE's Swing browser. Instead, I showed how to open an HTML file in a JEditorPane. The problem with that approach, as described in that blog entry, is that hyperlinks in the page cannot be clicked and followed. They're visible as blue underlined text, but they can't be clicked. However, about a week ago, there was a discussion about this point on dev@openide.netbeans.org (which is the NetBeans mailing list dedicated to module development and rich-client application development). There somebody referred to this very helpful discussion on JavaRanch: JEditorPane problem.

Basically, you need to teach the JEditorPane about hyperlinks. And for that purpose you just use the JEditorPane's addHyperlinkListener method. Here's all you need to do:

UpperEditorPane.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent ev){
try{
if (ev.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
LowerEditorPane.setPage(ev.getURL());
}catch(IOException ex){
ex.printStackTrace(System.err);
}
}
});

So, what this does is, when you click the link in the UpperEditorPane, the LowerEditorPane is set to contain the HTML page that is referenced by the hyperlink. (Alternatively, you could create a new TopComponent and display the HTML page there.) This is how it could look in the user interface:

Then, if you want to have the lower JEditorPane understand what to do with hyperlinks, you need to implement another addHyperlinkListener method for that purpose. (And, by the way, unless you use the HTMLEditorKit, the HTML file above would just be displayed as text, not as HTML.)

To give some context, the highlighted code below is all I added to the TopComponent class (created via a wizard) to create the above effect:

private TestModuleTopComponent() {
initComponents();
setName(NbBundle.getMessage(TestModuleTopComponent.class, "CTL_TestModuleTopComponent"));
setToolTipText(NbBundle.getMessage(TestModuleTopComponent.class, "HINT_TestModuleTopComponent"));//set HTML Editor Kit for UpperEditorPane and get static HTML doc from Bundle:
UpperEditorPane.setEditorKit(new HTMLEditorKit());
try {
String s = NbBundle.getMessage(TestModuleTopComponent.class, "WelcomeDocument");
URL demoDetailsURL = new URL(s);
UpperEditorPane.setPage(demoDetailsURL);
} catch (IOException ex) {
ex.printStackTrace();
}
//set HTML Editor Kit for LowerEditorPane:
LowerEditorPane.setEditorKit(new HTMLEditorKit());
//listen for clicks in UpperEditorPane:
UpperEditorPane.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent ev){
try{
if (ev.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
LowerEditorPane.setPage(ev.getURL());
}catch(IOException ex){
ex.printStackTrace(System.err);
}
}
});
//listen for clicks in LowerEditorPane:
LowerEditorPane.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent ev){
try{
if (ev.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
LowerEditorPane.setPage(ev.getURL());
}catch(IOException ex){
ex.printStackTrace(System.err);
}
}
});

}

Join the discussion

Comments ( 2 )
  • Sandip Monday, June 26, 2006

    You say that "(And, by the way, unless you use the HTMLEditorKit, the HTML file above would just be displayed as text, not as HTML.)"

    Normally you should not have to do that. Normally the html text in a JEditorPane is shown as rendered html when the JEditorPane's content type is ("text/html") and editable property is set to false. This lets the JEditorPane choose the right editor kit for the mime type ("text/html"). In other words you should not be setting the editor kit explicitly. However bug # 57994 prevents this from working correctly inside Netbeans.

    So I guess you ran into a work around for the bug accidently :)

  • Geertjan Tuesday, June 27, 2006
    Hmm. Well, I didn't set the content type to "text/html". I set editable property to false and then set the HTMLEditorKit as described, i.e., in the code. Maybe that's why I need to do it in the code, because I didn't set the content type property. (I prefer doing things in the code to setting properties, anyway. There are just too many of those properties. I always forget what's been set/not set and that very long list is not very manageable.)
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.