jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Problem with repaint() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with repaint()" Watch "Problem with repaint()" New topic

Problem with repaint()

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
Hi there,

I am calling repaint() on an object of class that extends JPanel.
Directly after calling repaint(), I need to call a method, see below:

However, the board isn't being repainted, till after someMethod returns.

I tried using...

But that didn't work either. Can anyone help me solve this, perhaps this needs to go in the threads section, but I'm not sure.

Thanks for any help
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

repaint() tells the Swing event thread to paint the component -- the call to paintComponent() does not happen on the thread that calls repaint(). If you want to do something after the painting is completed, then either your paint method needs to notify the thread that called repaint() somehow, or you need to schedule that activity to run on the event thread using SwingUtilities.invokeLater() or invokeAndWait(), or perhaps most simply, you can do that thing in the paintComponent() method itself.

[Jess in Action][AskingGoodQuestions]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
I tried calling the method at the end of the paintComponent method, but it was still running the method first before it got to draw to the screen.

I will take a look at the other options you mentioned. But I do have a thread allergy, which doesn't help
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
Hey.. I got it working (First Time!!!)
I basically put got it to output 1 to 1000 AFTER drawing the screen:

I don't fully understand it, but I get the jist of it, thanks
[ April 18, 2006: Message edited by: colin shuker ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem with repaint()
It's not a secret anymore!