Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

Work/paint/thread mixing

Thread view: 
TheDD - 21 Dec 2003 11:00 GMT
Hello all,

I'm developping an applet which upload a file on a ftp server.

On the applet i have one "Go" button. And when the user click on it, it
starts the upload.

For painting/refreshing/drawing problem, i create a thread for the upload.

But i also have a progress bar on the Applet Frame. So i don't know how to
mix that.

|       GUI thread           | |           upload thread         |
|->window need refresh       | | while (1) {                     |
|->progress bar value changed| |  applet.progressbar.setValue(); |
| recently enough            | |  bos.write();                   |
|                            | | }                               |

1/ I would like to "invalidate" the progress bar area when i change its
value. But calling applet.jProgresseBar.setValue() from the upload thread
seems ugly because i don't know what can happen if the gui thread is
already re-painting (is it thread safe?).
2/ I would like that the applet is repainted when some window go over the
browser window and then is removed (which is not the case actually).
3/ I don't want to manually call paint() each time the jprogressbar value
change, beacause i think it's utgly. I just want to warn the applet that
for the next draw (caused by time or sooner by something else), it take the
progress bar value change.

If somebody can tell me what is the best way to do that.

Signature

TheDD

TheDD - 21 Dec 2003 22:41 GMT
> Hello all,

> I'm developping an applet which upload a file on a ftp server.

> On the applet i have one "Go" button. And when the user click on it, it
> starts the upload.

> For painting/refreshing/drawing problem, i create a thread for the upload.

> But i also have a progress bar on the Applet Frame. So i don't know how to
> mix that.

>|       GUI thread           | |           upload thread         |
>|->window need refresh       | | while (1) {                     |
>|->progress bar value changed| |  applet.progressbar.setValue(); |
>| recently enough            | |  bos.write();                   |
>|                            | | }                               |

> 1/ I would like to "invalidate" the progress bar area when i change its
> value. But calling applet.jProgresseBar.setValue() from the upload thread
[quoted text clipped - 6 lines]
> for the next draw (caused by time or sooner by something else), it take the
> progress bar value change.

> If somebody can tell me what is the best way to do that.

If somebody is interested, i simulate "PostThreadMessage" calls with custom
messages by two classes:

// Update GUI data (and invalidate applet area)
class DoUpdateGUI implements Runnable {
 private FTPUploadApplet applet;
 private int value;

 public DoUpdateGUI(FTPUploadApplet applet, int value) {
   this.applet = applet;
   this.value = value;
 }

 public void run() {
   applet.jProgressBar.setValue(val);
 }
};

class UploadDone implements Runnable { ... };

and from the worker ftp upload thread:

while () {
 DoUpdateGUI r1 = new DoUpdateGUI(progressBarValue);
 SwingUtilities.invokeLater(r1);
}

UploadDone r2 = new UploadDone(message);
SwingUtilities.invokeLater(r2);

It works, don't know if it's clean (not really since there's a new() call
every 1024 byte send).

I don't know of calling setValue force a redraw instantly, i hope not.

Signature

TheDD                             http://www.epita.fr/~mancel_d
Ing2 Groupe B2
EPITA Promotion 2005

Adam - 22 Dec 2003 07:41 GMT
> I'm developping an applet which upload a file on a ftp server.
>
[quoted text clipped - 10 lines]
> seems ugly because i don't know what can happen if the gui thread is
> already re-painting (is it thread safe?).

I wouldn't worry about that. Even if it is not synchronized in any way
(which I doubt very much) there is nothing that can go wrong, you are just
assigning some new value to some variable.
And doing that from your upload thread seems very reasonable for me.
You just take care of the model (upload percent) from your thread,
not the look (EDT will repaint).

> 3/ I don't want to manually call paint() each time the jprogressbar value
> change, beacause i think it's utgly. I just want to warn the applet that
> for the next draw (caused by time or sooner by something else), it take the
> progress bar value change.
That'r s right, you never call paint yourself, it's the AWT framework
that does it. All you do is call Component.repaint() which puts an
repaint-request event in the EventQueue of EDT, and the EDT
will take care of it when it gets to that event.

Adam


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.