Hyperlinks


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Hyperlinks

  1. #1
    Join Date
    Oct 2004
    Posts
    151

    Question Hyperlinks

    Hi everyone,

    i need to ask a question which is how does one add a hyperlink in a jtextpane. I am using the default htmldocument class as the default document for the jtextpane.

    What i need is for example a user enters some text in the jtextpane and the user highlights it and clicks a button in which a dialog appears to hyperlink the text to for example www.yahoo.com

    I also need a way in which when the user clicks on the hyperlink in the jtextpane
    the textpane to go to that specific web page.

    This is what i have in the button action listener so far

    TextPane1 is the instance of the JTextPane class
    htmlkit is the instance of the HTMLEditorKit class
    htmldoc is the instance of the HTMLDocument class

    htmlkit.insertHTML(htmldoc, TextPane1.getCaretPosition(),
    "<A href=''>" + "http://www.yahoo.com" + "</A>",0,0,HTML.Tag.A);

    When i use the above command line it seems that the word
    "http://www.yahoo.com" is inserted in the jtextpane as a hyperlink but when i click it the textpane does not go to the specified page and nothing happens

    This is what i am doing in the hyperlink action listener

    public void hyperlinkUpdate(HyperlinkEvent event)
    {
    URL url1 = event.getURL();
    HyperlinkEvent.EventType type = event.getEventType();

    if(type == HyperlinkEvent.EventType.ACTIVATED)
    {

    try
    {
    TextPane1.setPage(url1);
    }

    catch(Exception e)
    {
    e.printStackTrace();
    }

    }

    }

    The program compiles without errors but i need a way to insert a hyperlink and able to click it an later the jtextpane to be set to that page.

    Yes i know the jtextpane must be set to not-editable mode to browse which i have already done in the initilization of the code but did not include it above

    I hope some one can help me with this

    Thank You

    Your Sincerely

    Richard West

  2. #2
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Is this the code you are using? I copied it from what you posted above.

    htmlkit.insertHTML(htmldoc, TextPane1.getCaretPosition(),
    "<A href=''>" + "http://www.yahoo.com" + "</A>",0,0,HTML.Tag.A);


    Because the String is incorrect. Should be something like this(href was equal to empty string):

    Code:
          String url = "http://www.yahoo.com";
    
          htmlkit.insertHTML(htmldoc, TextPane1.getCaretPosition(),
          "<A href=\"" + url + "\">" + url +  "</A>",0,0,HTML.Tag.A);

  3. #3
    Join Date
    Oct 2004
    Posts
    151
    Hi everyone,

    Your code worked perfectly but i have a very stupid question. How do you unhyperlink the hyperlinked text?

    Thank You

    Yours Sincerely

    Richard West

  4. #4
    Join Date
    Oct 2004
    Posts
    25
    PlainDocument as a Document

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center