Results 1 to 2 of 2
Thread: insertHTML in a JEditorPane
- 05-05-2008, 11:05 AM #1
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
insertHTML in a JEditorPane
Hi all,
I have created a JEditorPane which I use in a chat client, with the purpose of interpreting HTML and supporting links. It's working, but I believe I havn't come up with the optimal solution for inserting new html code. I store new strings in a StringBuffer, and then after each new string is appended, I update the HTML content using setText. See my code below.
Optimally, I believe I would use something likeJava Code:package com.olof.chat; import java.awt.Color; import java.awt.Font; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; import javax.swing.JEditorPane; import com.olof.applet.AppletActions; public class EditorPaneHyperlinked extends JEditorPane implements HyperlinkListener { private AppletActions aa; private StringBuffer sb; public EditorPaneHyperlinked(AppletActions aa) { setContentType("text/html"); addHyperlinkListener(this); setEditable(false); setFocusable(true); this.aa = aa; sb = new StringBuffer(); } public void append(String message, Font font, Color color) { String html = "<span style=\"color:#" + Integer.toHexString(color.getRGB()).substring(2) + "; font-family:" + font.getFamily() + "; font-size:" + font.getSize() + "pt; font-weight:" + (font.getStyle() == font.BOLD ? "bold" : "") + "\">" + message + "</span>"; append(html); } public void append(String message) { System.out.println("Appending: " + message); sb.append(message); setText(sb.toString()); } public void clear() { sb = new StringBuffer(); setText(""); } public void hyperlinkUpdate(HyperlinkEvent ev) { System.out.println("hyperlink event"); System.out.println("type: "+ev.getEventType()); System.out.println("url: "+ev.getURL()); if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { System.out.println("goTo: "+ev.getURL()); //applet.goTo(ev.getURL(), "_blank"); aa.action(); } } public void newLine() { append("<br/>"); } }
However, if I do that, it does not interpret the HTML anymore. Perhaps I need to use a HTMLDocument, but how do I achieve that?Java Code:doc = getDocument; doc.insert();
- 05-05-2008, 12:30 PM #2
Member
- Join Date
- May 2008
- Posts
- 22
- Rep Power
- 0
You might want to use the Cobra HTML parser, a component of the pure-Java Lobo browser. (But don't ask me for details; I'm a Lobo newbie. :))
Similar Threads
-
[SOLVED] Hide the titlebar of a JEditorPane
By Eranga in forum AWT / SwingReplies: 1Last Post: 03-28-2008, 10:28 AM -
Copy n' Paste in JEditorPane wraps text in new css..?
By Sam in forum AWT / SwingReplies: 0Last Post: 02-06-2008, 03:55 PM -
jeditorpane help parsing html
By asifsolkar in forum Advanced JavaReplies: 4Last Post: 12-14-2007, 05:23 AM -
JEditorPane
By drmmr11 in forum Java AppletsReplies: 0Last Post: 08-02-2007, 06:08 PM -
Adding custom highlight to JEditorPane
By andrewb in forum AWT / SwingReplies: 0Last Post: 06-22-2007, 06:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks