?
Solved

Streaming HTML in a JTextPane (etc.)

Posted on 2000-01-06
3
Medium Priority
?
345 Views
Last Modified: 2009-07-29
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.
0
Comment
Question by:memesis
  • 2
3 Comments
 
LVL 2

Expert Comment

by:meming
ID: 2330537
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".
0
 
LVL 9

Accepted Solution

by:
Ovi earned 400 total points
ID: 2331259
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();
      pane.setEditorKit(kit);
      doc = (HTMLDocument)kit.createDefaultDocument();
                  
      //The Document class does not yet handle charset's properly*/
     
      doc.putProperty("IgnoreCharsetDirective",Boolean.TRUE);
      Loader l = new Loader();//custom class wich read an html String from server
      pane.setText(l.result);
      doc = (HTMLDocument) pane.getDocument();
      JScrollPane sp = new JScrollPane (pane);
      Container c = getContentPane();
      c.add(sp, BorderLayout.CENTER);
      sp.updateUI();
      c.repaint();
}
}


//CODE FOR READING HTML INTO JTextPane. AND IS WORKING
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'         kit.read(r,(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 !!");
                        }                            
0
 
LVL 9

Expert Comment

by:Ovi
ID: 2331269
"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.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month4 days, 15 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question