Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel paintcomponent in runnable

 
Muteking Tonello
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been wrapping my head for days around that. I want to make a circle move in a JPanel. I've set up a paintcomponent method with a graphic object and it's been repainted inside the EDT.
Oracle docs say that you can't draw with Swing outside EDT.
But then, how do I do it?


 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you don't need a Thread / Runnable. Use a javax.swing,Timer instead.

Oracle docs say that you can't draw with Swing outside EDT.
But then, how do I do it?

All Swing constructors and methods should be invoked on the EDT, with the exception of the very few that are documented as being thread-safe. And for that, consult the Java 7 documentation; several methods that were earlier documented as thread-safe never were, and the documentation has been corrected in 7.

Once you use a Timer, you won't have a problem about firing updates on the EDT, but you may like to learn more about the techniques for doing so. You'll find all you need in the tutorial on Concurrency in Swing.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and since this is about Swing I'll move this thread to where we usually discuss such matters.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic