Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling revalidate/repaint in the same method?  RSS feed

 
Et Phillips
Greenhorn
Posts: 2
Eclipse IDE MySQL Database Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I am having trouble with updating a JFrame to reflect changes made to a JPanel, itself inside a JScrollPane. I'm trying to call revalidate() and repaint() on the frame twice in the same method, with only the latter taking effect. I think the problem is that the method is ignoring the middle step of setting the JScrollPane's contents, and just heads off after the finished result. Is there a way to enforce that it renders the middle step?

Essentially, I have a JScrollPane which is a viewport for a lobobrowser FramePanel (itself an extension of JPanel) which paints and revalidates fine. But it is slow - it takes some time to render the html, so I want to display a different JPanel in my JScrollPane until the FramePanel has finished rendering. Since this a little procedural piece of code it's all in the same method.




As you can see, I've been a bit liberal (over-zealous?) with my approach to revalidate() and repaint().
Any help greatly appreciated!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to wrap the two lines inside the inner try block in a SwingUtilities#invokeLater(...)

edit: along with the whole try-catch, of course
 
Et Phillips
Greenhorn
Posts: 2
Eclipse IDE MySQL Database Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Darryl. That works brilliantly and without too much of that nasty Thread manipulation business.
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait, why? Isn't it already being performed by the EDT?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is, but not in the desired sequence. Wrapping the client code in invokeLater(...) allows the Swing code triggered by revalidate() and repaint() to complete first.

Disclaimer: I've never actually stepped through the code, but this approach has worked for many use cases.
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh I see. Thanks Darryl.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!