• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread in paintComponent doesn't paint

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ranchers,

I found the stuff.

Start reading ...


Yours,
Bu.
reply
    Bookmark Topic Watch Topic
  • New Topic