freesoft_2000 9 Practically a Master Poster

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.