2 Replies Latest reply on May 10, 2012 5:42 AM by StanislavL

    How to insert hyperlinks in RTF document shown in JEditorPane?

      This is a compound question so bear with me on this one :)

      What I need is to insert an hyperlink in a JEditorPane; Store it as RTF code; Retrieve it; Show it and click on it to go the the destination.

      1) So, first question, is there some method to automatically add the hyperlink and will it be saved in RTF with RTFEditorKit? (I'm actually using AdvancedRTFEditorKit but an answer to either will be enough)

      2) I tried a method that adds what appears as a link but the document that is stored doesn't have the necessary RTF codes, only the formatting that makes it look like a link. I also checked the actual RTF codes I'd need to add and they seem easy to do. So, is there a way to add the actual underlying RTF codes to the document while it's in the JEditorPane?

      I know the question(s) isn't too specific, if you can shed some light over the whole thing and point me in the right direction, it'd be great!
        • 1. Re: How to insert hyperlinks in RTF document shown in JEditorPane?
          Just a follow up to my own question. I found a way to answer my question 2) and I'm now storing the rtf code for hyperlinks. Now, when I show the document in JEditorPane with RTFEditorKit, I get the name of the hyperlink with no formatting indicating that it's a hyperlink.

          Example. The link "http://www.google.com" with the name "Google", only shows "Google" in plain text.

          Is it because the components I'm using don't support hyperlinks from RTF files even though they're supported from HTML files?

          Is there any way you can see this working? Is it possible to include something inside the JEditorPane that the user can click, instead of the hyperlink?
          • 2. Re: How to insert hyperlinks in RTF document shown in JEditorPane?
            You can add attribute marker as well as formatting to some inserted text. E.g. when you call insertString() pass the formatting and the URL marker attribute in the AttributeSet. Then on click (or double click) listener check character Element (via the method of StyledDocumnet). If the URL marker attribute is here just get the URL value and do your action.

            You have to adapt RTFReader/writer though to handle the attribute though.