[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1326
  • Last Modified:

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!
0
sciuriware
Asked:
sciuriware
  • 15
  • 10
  • 8
1 Solution
 
girionisCommented:
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
 
objectsCommented:
use:

editor.getDocument().insertString(pos, s, null)
0
 
sciuriwareAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
try setting the content type setContentType()
0
 
girionisCommented:
Try casting the document to an HTMLDocument.
0
 
sciuriwareAuthor Commented:
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
 
objectsCommented:
"text/html"
0
 
sciuriwareAuthor Commented:
OK, how often?
JOOP!
0
 
objectsCommented:
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
 
sciuriwareAuthor Commented:
To girionis: HTMLDocument has no appropriete "insert text" or "append text",
To objects: neither work (text/html, <html>).
;JOOP!
0
 
objectsCommented:
If you can post a small compilable example I'll have a play with it here if you like.
0
 
sciuriwareAuthor Commented:
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
 
girionisCommented:
Please let us know. I am also curious if it can be done.
0
 
objectsCommented:
Try using the HTMLDocument classes insertXXX() methods.
0
 
objectsCommented:
or the HTMLEditorKit's insertHTML() method.
0
 
sciuriwareAuthor Commented:
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
 
girionisCommented:
> 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
 
sciuriwareAuthor Commented:
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
 
girionisCommented:
> 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
 
sciuriwareAuthor Commented:
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
 
girionisCommented:
>  j.getDocument( ).insertString(j.getDocument( ).getLength( ), {addition}, null);

So this finally worked?
0
 
sciuriwareAuthor Commented:
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
 
sciuriwareAuthor Commented:
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
 
girionisCommented:
I guess I was right then... You have to load up the whole string again.
0
 
objectsCommented:
> You have to load up the whole string again.

No you can use the html insert methods.
0
 
sciuriwareAuthor Commented:
I'd love to see how you can do that, objects.
;JOOP!
0
 
objectsCommented:
I posted the relevant method earlier, I'll see if I can find an example.
0
 
sciuriwareAuthor Commented:
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
 
sciuriwareAuthor Commented:
I found some sample code on the net but I couldn't even get them to display.
I am puzzled.
;JOOP!
0
 
objectsCommented:
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
 
sciuriwareAuthor Commented:
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
 
sciuriwareAuthor Commented:
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
 
girionisCommented:
Thank you for accepting. :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 15
  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now