🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9685

Re: What class to use for a StatusBar?

#9685
From: Peter den Haan
Date: Wed, 16 Feb 2000 00:00
36 lines
2118 bytes
Sweder Schellens wrote in message <88bfs8$jd01@bornews.borland.com>...

>When the user selects a menu option I search the harddisk for a certain
>file.
>During the search I want to display the directories I'm scanning.
>
>I do this using:
>
>JLabel statusBar = new JLabel();
>statusBar.setText("Scanning " + directory.toString());
>
>The problem is that the status bar doesn't display anything at all.


Swing is not threadsafe; all Swing code executes in a single thread, the
event dispatch thread. If your code is run from a GUI event handler (eg
ActionListener), it also executes in this thread. While your code is
executing, the GUI will not respond to events or be updated.

If I'm not mistaken (I might be) the setText you're using changes the label
text and posts a repaint event to the Swing event queue. This event will not
be executed until after your code finishes.

Lengthy operations should really execute in a thread of their own. They then
cannot directly call most Swing component methods, because these are not
threadsafe. There are two ways to communicate status and results back to the
GUI: (1) you can use a Timer to read your thread's status at regular
intervals; (2) the thread itself can use SwingUtilities.invokeLater() and
SwingUtilites.invokeAndWait() to safely call Swing component methods.

The Sun website has good articles on Swing and threads. You can also find
the SwingWorker class there, which makes spawning a worker thread and
communicating results back a bit easier.

 - Peter
Technical Details
Message-ID: <88drvf$lal2@bornews.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <88b0si$j5j6@bornews.borland.com> <uael2g2p1.fsf@sds.no> <88bfs8$jd01@bornews.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,85b51552b2a3f7ef
X-Google-Attributes: gid111d45,public
From: "Peter den Haan" <pieterh@spam.free.net>
Subject: Re: What class to use for a StatusBar?
Date: 2000/02/16
Message-ID: <88drvf$lal2@bornews.borland.com>
X-Deja-AN: 586459716
References: <88b0si$j5j6@bornews.borland.com> <uael2g2p1.fsf@sds.no> <88bfs8$jd01@bornews.borland.com>
Organization: Another Netscape Collabra Server User
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Newsgroups: borland.public.jbuilder.java.swing