Results 1 to 2 of 2
  1. #1
    olbion is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default 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.

    Java 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/>");
    	}
    }
    Optimally, I believe I would use something like
    Java Code:
    doc = getDocument;
    doc.insert();
    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?

  2. #2
    Jesdisciple is offline Member
    Join Date
    May 2008
    Posts
    22
    Rep Power
    0

    Default

    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

  1. [SOLVED] Hide the titlebar of a JEditorPane
    By Eranga in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2008, 10:28 AM
  2. Replies: 0
    Last Post: 02-06-2008, 03:55 PM
  3. jeditorpane help parsing html
    By asifsolkar in forum Advanced Java
    Replies: 4
    Last Post: 12-14-2007, 05:23 AM
  4. JEditorPane
    By drmmr11 in forum Java Applets
    Replies: 0
    Last Post: 08-02-2007, 06:08 PM
  5. Adding custom highlight to JEditorPane
    By andrewb in forum AWT / Swing
    Replies: 0
    Last Post: 06-22-2007, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •