Link to home
Start Free TrialLog in
Avatar of sciuriware
sciuriware

asked on

How to update JEditorPane without flickering

May be I overlooked but I can't find an .add(String) or .append(String) to JEditorPane or any of
its superclasses. So, all that remains is something like:

String s;
JEditorPane j;
........................
          s += ............
          j.setText(s);   // ?!?!?!?  

Is there another way?

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use:

editor.getDocument().insertString(pos, s, null)
Avatar of sciuriware
sciuriware

ASKER

Great!
But now it doesn't recognise the HTML in the addition.
So I get:

      Document d = editor.getDocument();
      .....
      d.insertString(d.getLength(), "A message appended<BR>", null);
      .....
      d.insertString(d.getLength(), "Another message added<BR>", null);

... doesn't flicker, but shows:

A message appended<BR>Another message added<BR>

The JEdtorpane was setup as:

      editor = new JEditorPane();
      editor.setEditable(false);
      editor.setEditorKit(new HTMLEditorKit());
and was chosen as a component because several messages carry icons etc.

;JOOP!
try setting the content type setContentType()
Try casting the document to an HTMLDocument.
What argument will I give "setContentType()"?
Is this call to be applied at every insertString()?
 and why is this necessary in spite of "editor.setEditorKit(new HTMLEditorKit());"?
;JOOP!
"text/html"
OK, how often?
JOOP!
just the once :)
not srue if it'll fix the problem, but worth a try.

another thing to try would be to wrap your html with <html></html>
To girionis: HTMLDocument has no appropriete "insert text" or "append text",
To objects: neither work (text/html, <html>).
;JOOP!
If you can post a small compilable example I'll have a play with it here if you like.
Completely apart from this problem my program doesn't produce the right output.
Before I can cut the problem out for you it must at least run (deadline).
I'll come back to it tomorrow.
;JOOP!
Please let us know. I am also curious if it can be done.
Try using the HTMLDocument classes insertXXX() methods.
or the HTMLEditorKit's insertHTML() method.
Sorry for the delay and thanks for being patient.
First of all I had to eliminate my unrelated program errors, second I had to meet a
deadline to at least deliver something useful.
I learned some interesting things:
1) I had to survey more than 250000 status files, that of course mostly were missing on my laptop;
the survey took only a minute (JAVA slow?) inclusive of the required report in HTML, but including
some 245000 icons to show the status. This produced a String of about 24Mb. As stated: in a minute!
2) the statement          editorpane.setText(thatString);                    took ..... 26 hours.
at least, then I had to kill it: the program fully used up its 512 Mb limit, but W2k couldn't cope with
720Mb virtual in only 512Mb physical memory ......................
3) when I simulated the clients situation, by creating some 240000 dummy status files, figures went:
processing: 14 minutes, showing in editorpane: 9 seconds. So I could deliver!

Now, this proves there is a good reason to show a progressbar while processing, or,
to add items to the editorpane as they are created.
Although the program (with progressbar) is good enough, I made a copy of the whole
project, to tackle the problem this EE question is all about.
I now will try your suggestions.

;JOOP!
> the statement          editorpane.setText(thatString);                    took ..... 26 hours.
> at least, then I had to kill it: the program fully used up its 512 Mb limit, but W2k couldn't
> cope with 720Mb virtual in only 512Mb physical memory ......................

Hehe, even if you do find a solution this could take a while :)
No, the definitive version runs on W2003 with 2Gb and almost all of the mentioned files present.
Then it's a matter of seconds (UNISYS ES7000).
Usually the programmer fails his/her tests because the real life situation has much more data.
In this rare case it was the other way round.
;JOOP!
> Usually the programmer fails his/her tests because the real life situation has much more data.
> In this rare case it was the other way round.

Rare case indeed :)
Sorry to be late again, but I've seen too much of the inside of hospitals last weeks.

   Well, I did review the whole problem:
JEditorPane j;
String s;
......
     s += "<some more HTML>";
     j.setText(s);
>>>>>>>>>>>>>>>>> flickers and flashes.

Building up the whole string and finally setting it at once was good enough for my customer, so I delivered that way.
Not for me, especially not in future cases, so:

    j.getDocument( ).insertString(j.getDocument( ).getLength( ), {addition}, null);

lost its HTML content.

     j.setContentType("text/html");

... no change.

     j.setText("<HTML>");

... for a start: no change.

     casting the document to an HTMLDocument ...

... no change.

     I studied the insert methods of HTMLDocument, but those need "elements" as guideline.

I reduced the whole project to only 5 modules of 2 pages each, but I fear that's still to much.
The next thing I want to do is write a small program from scratch (like objects suggested),
that at least is apart from all the library stuff that may obscure the problem,
but may be caused some problem.
;JOOP!


>  j.getDocument( ).insertString(j.getDocument( ).getLength( ), {addition}, null);

So this finally worked?
No, it lost the HTML characteristics too, so
           "ONE<BR>", added "TWO<BR>"
originally showed as

ONE
TWO

and by that code again as:

         ONE<BR>TWO<BR>

I go off line now, have to visit my father in hospital; tonight I will enter some code.
;JOOP!
Hello,
          here are two minimum sources; the first one does the job but to some visual costs:
import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLEditorKit;

public class Problem1 extends JFrame
{
   public static void main(String[] argv)
   {
   Problem1 b = new Problem1();
   String s = "";
   JEditorPane j;

      try
      {
         b.setSize(800, 600);
         j = new JEditorPane();
         j.setEditable(false);
         j.setEditorKit(new HTMLEditorKit());
         b.getContentPane().add(j, BorderLayout.CENTER);
         b.setVisible(true);

         for(int i = 1;  i <= 10;  ++i)
         {
            s += "The <b>next</b> line is number " + i + "<BR>";
            j.setText(s);
            Thread.sleep(1000);
         }
      }
      catch(Exception e){ /* Pro Forma */ }
      System.exit(0);
   }
}
========== the second source does not flicker, but forgets about HTML:
import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.Document;

public class Problem2 extends JFrame
{
   public static void main(String[] argv)
   {
   Problem2 b = new Problem2();
   String s;
   JEditorPane j;
   Document d;

      try
      {
         b.setSize(800, 600);
         j = new JEditorPane();
         j.setEditable(false);
         j.setEditorKit(new HTMLEditorKit());
         j.setText("<HTML>");
         j.setContentType("text/html");
         d = j.getDocument();
         b.getContentPane().add(j, BorderLayout.CENTER);
         b.setVisible(true);
         
         for(int i = 1;  i <= 10;  ++i)
         {
            s = "The <b>next</b> line is number " + i + "<BR>";
            d.insertString(d.getLength(), s, null);
            Thread.sleep(1000);
         }
      }
      catch(Exception e){ /* Pro Forma */ }
      System.exit(0);
   }
}
===============
;JOOP!
I guess I was right then... You have to load up the whole string again.
> You have to load up the whole string again.

No you can use the html insert methods.
I'd love to see how you can do that, objects.
;JOOP!
I posted the relevant method earlier, I'll see if I can find an example.
I tried to understand the HTMLEditorKit from the 1.4.2 JAVADOC, but I fail to see how to
apply it to the above code sample.
;JOOP!
I found some sample code on the net but I couldn't even get them to display.
I am puzzled.
;JOOP!
The HTML document is stored as a structured document so to modify it it you need to update that structure and not insert text (the text has already been parsed).

Heres an example showing you to iterate thru the document that should demonstrate this:
http://www.javaalmanac.com/egs/javax.swing.text.html/GetLinks.html
objects, I studied the java Almanac pages and now I understand what's wrong with my approach.
However, my JEditorPane was filled with a huge table withtext and an icon in each cell to indicate statuses.
I can't find out how to update / extend such a structure, because I don't know how to search for the
proper entry, even if it was just after the last one added.
Before:
     <TABLE><BORDER=2><CENTER>
     <TR>
          <TD>first item</TD>
     </TR>
     </TABLE>
After:
     <TABLE><BORDER=2><CENTER>
     <TR>
          <TD>first item</TD><TD>second item</TD>
     </TR>
     </TABLE>

How do I position for addition of the second item?
The only thing I found is:

     int caret = jeditorpane.getCaretPosition();

But that 'caret' is initially at the end, I suppose.
I would like to close this question if I know how to insert table items at the right place.
I already know that my approach of building the entire text and then jeditorpane.setText(all);
is effective in most cases.
;JOOP!
Maybe everybody is busy, I feel I have to close this question.
Because of the complexity of updating a HTML text I chose the solution (!?) by girionis,
which does well in most environments.
;JOOP!
Thank you for accepting. :)