Skip to main content

[JAVA2D] calling repaint()

2 replies [Last post]
Anonymous

This is semi-swing related, but since there's not a swing-interest list and since it's also semi 2D related, I thought I'd try here.

I learned this past JavaOne that every swing app I have ever written has been broken, as I often do

public static void main(String[] args)
{
JFrame f = new JFrame();
f.setVisible(true);
}

when in fact I should be calling this from the event handling thread via a SwingUtilities.invokeLater() call.

My question is, can I call repaint() on a component from another thread, or does that also have to be called from within the event thread? Doesn't repaint just queue a request for later processing anyway? If so, why would I need to spawn a separate thread just to call the repaint?

Rob

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chet Haase

Hi Rob,

repaint() does the right thing - it queues up a request to call paint on the
component on the right thread at some later time. This means, of
course, that
repaint() is an asynchronous request; it will return to the caller as
soon as the
request has been posted, and not necessarily after the paint has
actually occurred.
In any case, feel free to use repaint() from whatever thread you want;
that's what
it's there for...

Chet.

Rob Ross wrote:

>This is semi-swing related, but since there's not a swing-interest list and since it's also semi 2D related, I thought I'd try here.
>
>I learned this past JavaOne that every swing app I have ever written has been broken, as I often do
>
>public static void main(String[] args)
>{
> JFrame f = new JFrame();
> f.setVisible(true);
>}
>
>when in fact I should be calling this from the event handling thread via a SwingUtilities.invokeLater() call.
>
>My question is, can I call repaint() on a component from another thread, or does that also have to be called from within the event thread? Doesn't repaint just queue a request for later processing anyway? If so, why would I need to spawn a separate thread just to call the repaint?
>
>Rob
>
>===========================================================================
>To unsubscribe, send email to listserv@java.sun.com and include in the body
>of the message "signoff JAVA2D-INTEREST". For general help, send email to
>listserv@java.sun.com and include in the body of the message "help".
>
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Nidel, Mike

Good news...
repaint() is asynchronous and thus can be called from any thread.

Having said that, it is possible to back yourself into a difficult
corner
when using multithreaded apps with synchronization. Most notably,
deadlock can
occur if your paint() requires synchronized objects/locks. Of course you
should avoid this whenever possible since it imposes a lock overhead in
your
paint thread and you would like to avoid this, but it isn't always
possible.
If your application hangs, then you may need to be careful about how you
call
repaint() (or SwingUtilities.invokeLater(), or
SwingUtilities.invokeAndWait()
for that matter).

I have been writing Swing apps the same way as you since the
beginning and though I've seen the info about using
SwingUtilities.invokeLater()
I never switched my apps and I haven't had any real problems.
Just my 2 cents there.

This is not at all on the topic of Java2D, but I figure help is help.

Mike

> -----Original Message-----
> From: Discussion list for Java 2D API
> [mailto:JAVA2D-INTEREST@JAVA.SUN.COM]On Behalf Of Rob Ross
> Sent: Tuesday, July 13, 2004 2:45 PM
> To: JAVA2D-INTEREST@JAVA.SUN.COM
> Subject: [JAVA2D] calling repaint()
>
>
> This is semi-swing related, but since there's not a
> swing-interest list and since it's also semi 2D related, I
> thought I'd try here.
>
> I learned this past JavaOne that every swing app I have ever
> written has been broken, as I often do
>
> public static void main(String[] args)
> {
> JFrame f = new JFrame();
> f.setVisible(true);
> }
>
> when in fact I should be calling this from the event handling
> thread via a SwingUtilities.invokeLater() call.
>
> My question is, can I call repaint() on a component from
> another thread, or does that also have to be called from
> within the event thread? Doesn't repaint just queue a request
> for later processing anyway? If so, why would I need to spawn
> a separate thread just to call the repaint?
>
> Rob
>
> ==============================================================
> =============
> To unsubscribe, send email to listserv@java.sun.com and
> include in the body
> of the message "signoff JAVA2D-INTEREST". For general help,
> send email to
> listserv@java.sun.com and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".