Solved

Flickering of a JEditorPane in a JScrollPanel

Posted on 2004-08-02
15
992 Views
Last Modified: 2008-01-09
Hi Experts, well I have a problem.  I have implemented an html JEditorPane in a JScrollPanel, and it works fine, but the problem is I am adding HTML code to this JEditorPane .  I have tried making it doublebuffered, playing with the Opaqueness, and even tried overloading paint, but that is only called when the JApplet is drawn or the screen is refreshed.  I get a problem when I post quickly to the JEditorPane the scrolling starts scrolling, but it flickers and I can see images from above in the middle of the screen, which is odd since they are only on the sides.  If I add things slowly, it still happens only occasionally.  Is there a way maybe to override the paint function of the JEditorPane?  Please help, thanks in advance.

Kes.
0
Comment
Question by:kesea
  • 8
  • 6
15 Comments
 
LVL 9

Expert Comment

by:keteracel
ID: 11699225
you couldn't post the code for the pane please?

Offhand, it  sounds like it's either doing too much so it's taking too long, or the caret position is changing making the scroll move...

Without demoing it I don't know what exactly the symptoms are...
0
 

Author Comment

by:kesea
ID: 11700369
Ok I just grabbed the parts that are screwing up, I have an JEditorPane that is in a JScrollPane and when I post HTML data to it (images and text) it is ok sometimes, but intermittently it flickers.  It is annoying, especially if you start sending text quickly to the JEditorPane, any suggestions?


    JScrollPane jScrollPane1 = new JScrollPane();
    JEditorPane output = new JEditorPane();

    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane1.getViewport().setBackground(Color.white);
    jScrollPane1.setAlignmentX((float) 0.0);
    jScrollPane1.setAlignmentY((float) 0.0);
    jScrollPane1.setDoubleBuffered(true);
    jScrollPane1.setBounds(new Rectangle(12, 12, 470, 374));
    output.setForeground(Color.white);
    output.setName("");
    output.setOpaque(false);
    output.setCaretColor(Color.white);
    output.setSelectionColor(Color.white);
    output.setContentType("text/html");
    output.addComponentListener(new ChatApplet_output_componentAdapter(this));
    output.setDoubleBuffered(true);
    output.setCaretPosition(0);
    output.setMargin(new Insets(3, 3, 3, 3));
    output.setText("<html><head></head><body></body></html>");
    jScrollPane1.getViewport().add(output, null);

  public void ShowReceivedMessage (String I_Message_ac)
  {
     String htmlText_ac;
     htmlText_ac = output.getText();
     StringBuffer text_ac = new StringBuffer();
     text_ac.append(htmlText_ac);
     if (htmlText_ac.indexOf("</body>") >= 0)
     {
        int insertAt_i = htmlText_ac.indexOf("</body>");
        text_ac.insert(insertAt_i, buildHtmlMessage(I_Message_ac));
        output.setText(text_ac.toString());
     }
  }

  String buildHtmlMessage(String I_message_ac)
  {
     String messageText_ac = new String();
     StringBuffer temp_ac = new StringBuffer(I_message_ac);

     messageText_ac = "<table border=0 bordercolor=white><tr><td><table border=0 bgcolor=\"dddddd\
                      "width=438><tr> <td width = 45 align=center valign=top wrap>" +
                      "<img src=\"" + mUserData_t.preferedIconName_ac + "\">" +
                      "</td><td wrap align=left width = 393 valign=top >" +
                      "<b>" + mUserData_t.userName_ac + " says:</b>" + "<br> " +
                      temp_ac.toString() +
                      "</td></tr></table> </tr></td></table>"; //+ mMessageBreakImage_ac;
     return messageText_ac;
  }

0
 
LVL 92

Expert Comment

by:objects
ID: 11700633
where is ShowRecievedMessage being called from ie. what thread?
it may be worth trying running it on the event dispatch thread. See EventQueue.invokeLater(), and invokeAndWait()
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 11700647
Another idea (if desperate) would to be detach the editor from the scroll pane before doing the update, then reattching it once done with the viewport set at the same position when detached (if that makes sense).
0
 

Author Comment

by:kesea
ID: 11700850
The ShowReceivedMessage is being called from a socket when it receives a message that way.  

Basically what I am trying to do is get a nice scrolling chat message box, is there a better way to do this?  I found the easiest way was to just put the html in a JEditorPane and have it scroll, but it is goofing up.  

I appreciate the help, I have a few questions for your responses:
1)How would putting this in the event dispatch thread help?  Would it only call the refresh for the scrolling JEditorPane once?  

2)So if I understand your 2nd idea, I would unattach the JEditorPane from the JScrollPane then update the JEditorPane (so it doesn't scroll), and then re-attach it to the JScrollPane and then move it to the bottom?  I actually tried not making the JEditorPane part of the JScrollPane and just watched it update, and the same kind of blips happened on it, then I thought it was due to the double buffering of the JEditorPane, that is why I was trying to just override the paint of the JEditorPane.  

Thanks for your help.

0
 
LVL 92

Expert Comment

by:objects
ID: 11700862
1) Swing is single threaded and as such not thread safe.

2) so you are saying that the problem is not related to the scrolling, purely the editorpane.
How would you fix it by subclassing?
0
 

Author Comment

by:kesea
ID: 11707705
1) Ok, so you think the posting of the new JEditorPane, because I am just adding onto it and reposting the whole Text would effect it?  

2) It looks like the problem is in the editorpane.  Yeah I guess I was a little off by thinking I could fix it by double buffering just the Editorpane.  Is there a better way to put images and text in a scrolling window?  I tried the JList, but it was really ugly.  The HTML gives me the flexibility to make it look really good.  But I need it to scroll.  Can you put HTML in a JLIST?  
0
 
LVL 92

Expert Comment

by:objects
ID: 11710508
have you tried moving the ShowRecievedMessage to the EDT?
0
 

Author Comment

by:kesea
ID: 11712830
Hmmm, how do I do that exactly?
0
 
LVL 92

Expert Comment

by:objects
ID: 11712845
using EventQueue.invokeLater(), and invokeAndWait()
0
 

Author Comment

by:kesea
ID: 11768671
Ok, it is the JEditPane flickering with an update of the HTML.  Is there another way to do this, can I use a JList with HTML?  Would that solve my problem?
0
 
LVL 92

Expert Comment

by:objects
ID: 11768698
I think thats because you effectively replace the entire page, so the whole thing gets re-rendered.
Instead try inserting the html into the existing document, using HTMLEditorKit and/or HTMLDocument classes.

As far as using a different component goes, it depends what it is you are trying to achieve.
0
 

Author Comment

by:kesea
ID: 11769211
How can you do that?  I was just looking up how to do that, just to insert the HTML into the existing document.  Do you just do an add to the document?  I was always getting the whole page, finding the </body> tag and inserting my html ahead of that.  Can I just add <html><body>my stuff</body><html> into the JEditorPane?  There doesn't seem to be much info on this on the web.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11769237
Have a look at the insertHTML() method in JEditorPane (sorry haven't actually used it so don't have an example).
0
 
LVL 92

Expert Comment

by:objects
ID: 11770042
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

679 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