I've got a program outputs some URLs to a JEditorPane. I want the URLs to be hyperlinks. The program will basically output the URLS to the JEditorPane as if it were a log.
I've got it somewhat working, but it's not hyperlinking the URLs.
Here's the code I have:
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new HyperlinkListener() {
//listener code here
});
//some other code here
StyledDocument document = (StyledDocument) editorPane.getDocument();
String url = "http://some url";
String newUrl = "\n<a href=\""+url+"\">"+url+"</a>\n";
document.insertString(document.getLength(), "\n" + newUrl + "\n", null);
Instead of http://example.com/ it's outputting:
<a href="http://example.com/">http://example.com/</a>
If I don't use a StyledDocument and just do editorPane.setText(newUrl)
it does correctly hyperlink the URLs, but it has the obvious problem that setText will replace whatever was already there.