This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JPanel paintcomponent in runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel paintcomponent in runnable" Watch "JPanel paintcomponent in runnable" New topic
Author

JPanel paintcomponent in runnable

Muteking Tonello
Greenhorn

Joined: Jul 18, 2013
Posts: 8
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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Oh, and since this is about Swing I'll move this thread to where we usually discuss such matters.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPanel paintcomponent in runnable
 
Similar Threads
Rotating a JWindow with multiple JPanels
JFrame not resizing when JPanel changes size
My first time trying to make a simple tile-based java game
Popup menu not responding with mouse event
A flatland puzzle