Streaming HTML in a JTextPane (etc.)

Although there's plenty of support for loading a single static HTML page into a JTextPane and friends, I'm looking for a way to push arbitrary HTML into an existing pane and have it render the new HTML as it gets it, without replacing what's already there.  I've experimented with the Document stuff with no success; any pointers in the right direction would be appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

As I know Swing only deals with well formated HTML code. If you want part of a page to be rendered before the rest of the page, you probably will get exceptions. Say "</HTML> tag not found".
First step : Make your JTextPane editable.
Second step : read the original swing sources and you will find the way to do this. Or you can use this code (EXTRACTED code from an HTML editor applet) :

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class Editor extends JApplet {
  public HTMLEditorKit kit;    
  public static HTMLDocument doc;//original doc este de tipul StyledDocument
  public static JTextPane pane;

public void init() {
      pane = new JTextPane();
      kit = new HTMLEditorKit();
      doc = (HTMLDocument)kit.createDefaultDocument();
      //The Document class does not yet handle charset's properly*/
      Loader l = new Loader();//custom class wich read an html String from server
      doc = (HTMLDocument) pane.getDocument();
      JScrollPane sp = new JScrollPane (pane);
      Container c = getContentPane();
      c.add(sp, BorderLayout.CENTER);

Reader r;
int position = Editor.pane.getCaretPosition();
HTMLEditorKit kit = (HTMLEditorKit)Editor.pane.getEditorKit();
try {
         r = new StringReader(imported);
//insert at position given by variable 'position',(HTMLDocument)Editor.pane.getDocument(),position);
         Editor.pane.updateUI();                                                pos=((HTMLDocument)kit.createDefaultDocument()).getLength();
                                             Editor.pane.setCaretPosition(pos);//set the caret at the end of the document.
                                                                     }catch(BadLocationException e1) {
                              System.out.println("EditorToolBar - Copy button exception 1 !!");
                        }catch(IOException ex) {
                              System.out.println("EditorToolBar - Copy button exception 2 !!");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
"BUGS" : - for new document you must insert at the position 3. First are reserved for <html>, <head>, </head> tags (for an corect insertion into the body).
- after insertion Swing add two CR caracters at the begining and the end of the inserted text, so your view of the text will be not very great - delete them and call updateUI method. If you try to insert html objects, you must know that the length of this is 1, so be carefull what you delete. If insertion of objects is not working as you expect to, try to embed them in <form> tags.

For more questions please ask.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.