-
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
-
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);
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks