Results 1 to 3 of 3
  1. #1
    goodlily is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Can HyperlinkListener detect the click on the anchor in JEditorPane?

    Hi:

    I have a html document and I want to put it inside a JEditorPane to review. I only need to read so the JEditorPane has 'setEditable(false)'. There are many anchors (<a>) inside this html document which points to somewhere within the same html document. I would like 'the click on the anchor wil make JEditorPane to scroll to the place where the anchor points to.

    Can I write a HyperlinkListener for this JEditorPane to make this work? Anybody share the source code?

    Appreciate
    Lily

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: Can HyperlinkListener detect the click on the anchor in JEditorPane?

    Not sure, but I think this code might work. I think the frame hyperlink event is for internal link as oppose to loading a completely new page in the else condition.

    Java Code:
    public HyperlinkListener createHyperLinkListener() { 
     	return new HyperlinkListener() { 
     	    public void hyperlinkUpdate(HyperlinkEvent e) { 
     		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 
     		    if (e instanceof HTMLFrameHyperlinkEvent) { 
     			((HTMLDocument)html.getDocument()).processHTMLFrameHyperlinkEvent( 
     			    (HTMLFrameHyperlinkEvent)e); 
     		    } else { 
     			try { 
     			    html.setPage(e.getURL()); 
     			} catch (IOException ioe) { 
     			    System.out.println("IOE: " + ioe); 
     			} 
     		    } 
     		} 
     	    } 
     	}; 
         }

  3. #3
    goodlily is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Re: Can HyperlinkListener detect the click on the anchor in JEditorPane?

    that is not working. I think the right code is

    String ref = evt.getURL().getRef();
    if (ref != null && ref.length() > 0)
    {
    System.out.println("reference to scroll to = " + ref);
    scrollToReference(ref);
    }

    However, the 'evt.getURL' returns null. I am not sure what I am missing. Anybody knows? Please help.

    Thanks,
    lily

Similar Threads

  1. Replies: 1
    Last Post: 06-27-2011, 06:11 PM
  2. Replies: 16
    Last Post: 11-17-2010, 01:02 PM
  3. anchor tag in html
    By meghana in forum Eclipse
    Replies: 3
    Last Post: 08-18-2010, 03:21 PM
  4. How to detect double click with a JFrame
    By Jary316 in forum New To Java
    Replies: 3
    Last Post: 01-05-2009, 08:39 PM
  5. Demonstrating the HyperlinkListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •