Solved

How to update JEditorPane without flickering

Posted on 2004-08-10
33
1,166 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:sciuriware
  • 15
  • 10
  • 8
33 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 11760077
AFAIK you will have to load the full page again. You cannot set any text, since the purpose of the JEditorPane is to display styled text. You can always create your own class that does this though by subclassing JEditorPane and implements the StyledDocument.
0
 
LVL 92

Expert Comment

by:objects
ID: 11760092
use:

editor.getDocument().insertString(pos, s, null)
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11760231
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!
0
 
LVL 92

Expert Comment

by:objects
ID: 11760239
try setting the content type setContentType()
0
 
LVL 35

Expert Comment

by:girionis
ID: 11760247
Try casting the document to an HTMLDocument.
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11760333
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!
0
 
LVL 92

Expert Comment

by:objects
ID: 11760345
"text/html"
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11760361
OK, how often?
JOOP!
0
 
LVL 92

Expert Comment

by:objects
ID: 11760380
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>
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11760479
To girionis: HTMLDocument has no appropriete "insert text" or "append text",
To objects: neither work (text/html, <html>).
;JOOP!
0
 
LVL 92

Expert Comment

by:objects
ID: 11760516
If you can post a small compilable example I'll have a play with it here if you like.
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11760777
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!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11760786
Please let us know. I am also curious if it can be done.
0
 
LVL 92

Expert Comment

by:objects
ID: 11761064
Try using the HTMLDocument classes insertXXX() methods.
0
 
LVL 92

Expert Comment

by:objects
ID: 11769153
or the HTMLEditorKit's insertHTML() method.
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11808090
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!
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 35

Expert Comment

by:girionis
ID: 11808247
> 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 :)
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11808347
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!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808390
> 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 :)
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11863258
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!


0
 
LVL 35

Expert Comment

by:girionis
ID: 11863295
>  j.getDocument( ).insertString(j.getDocument( ).getLength( ), {addition}, null);

So this finally worked?
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11863341
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!
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11865319
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!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11865385
I guess I was right then... You have to load up the whole string again.
0
 
LVL 92

Expert Comment

by:objects
ID: 11865825
> You have to load up the whole string again.

No you can use the html insert methods.
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11867559
I'd love to see how you can do that, objects.
;JOOP!
0
 
LVL 92

Expert Comment

by:objects
ID: 11867567
I posted the relevant method earlier, I'll see if I can find an example.
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11901771
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!
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11902183
I found some sample code on the net but I couldn't even get them to display.
I am puzzled.
;JOOP!
0
 
LVL 92

Expert Comment

by:objects
ID: 11907619
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
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11926682
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!
0
 
LVL 24

Author Comment

by:sciuriware
ID: 11984673
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!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11989311
Thank you for accepting. :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Company 5 69
array6 challenfge 6 63
pairs challenge 5 44
java  and programming certification ? 4 65
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now