I'm using a JEditorPane document to be filled from a String. I execute the following code, but I do not understand why the following instruction returns always 0 for the document length.
Thank you in advance for your detailed explanations.
String xxx = "This is my text sample";
javax.swing.JEditorPane p = new javax.swing.JEditorPane();
p.setContentType("text/rtf");
EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
java.io.InputStream stream = new java.io.ByteArrayInputStream(xxx.getBytes());
kitRtf.read(stream, p.getDocument(), 0);
System.out.println(p.getDocument().getLength());
The reason why your code outputs zero is because your editor kit, the string, and the document in the editor pane are disjointed. There are no relationship between them, especially between the editor kit and the document. Look at a simple example without an editor kit first. It will show the relationship between the string you want to set and the document. The process is simple:
Without Editor Kit
This outputs 22.
Now that you know how to connect the string to the document, let's add the editor kit. When using an editor kit, you got to make sure there is a relationship or association between the document and the editor kit. For this:
With Editor Kit
This also outputs 22