Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread in paintComponent doesn't paint

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I made a test application to just paint a dot.
The paintComponent was overridden in an anonymous class of JPanel.

When I changed the line commented out (bold) in the following

... with a runnable and thread (italics).

The thread obviously runs ("dot" output in system.out), but the dot didn' show up again.

What am I doing wrong in the threading?
I looked up Sun tutorial about Swing but didn't found something about threading there.
Anyone knows where to look this up?


Yours,
Bu.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing is not thread safe. The Graphics object cannot be passed to different thread.
See Threads and Swing.
[ December 18, 2006: Message edited by: Vlado Zajac ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's already a thread in charge of painting all components that calls paint() (which then calls paintComponent()) - grabbing the graphics reference and painting to it in another thread is not going to work because there's no reference to any component or anything once the other thread gets around to calling g.paintOval(), the graphics reference might be painting another component, have a different clip region, be disposed of, etc. by this point anyway.

Swing is a single threaded model - look up some stuff about the event thread in Swing, SwingUtilities.invokeLater(), SwingUtilities.invokeAndWait(), etc.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ranchers,

I found the stuff.

Start reading ...


Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic