Thread View: borland.public.jbuilder.java.swing
4 messages
4 total messages
Started by Tony
Sun, 22 Jan 2006 07:40
progress bar during a critical operation in the event dispatch thread
Author: Tony
Date: Sun, 22 Jan 2006 07:40
Date: Sun, 22 Jan 2006 07:40
48 lines
1850 bytes
1850 bytes
Dear all,
I have a question that bothers me. I think I know the answer, but
it's simplicity is making me doubt my reasoning.
I would greatly appreciate if somebody more experienced
could chime in with an opinion.
Tony
Sample code that explains my problem:
startButton.addActionListener(new Action Listener()
{
public void actionPerformed(ActionEvent e)
{
method1();
method2();
}
}
);
method1()
{
/*
I have some long-running operations in this method.
Is it true that there is no way to display and
update a progress bar in this method?
*/
}
method2()
{
/*
This method contains operations that must not
be performed until method1 is finished
*/
}
My reasoning:
1. Display and updates of the progress bar are UI operations
which must take place on a Swing thread.
2. Swing thread will not resume until actionPerformed method
exits.
Re: progress bar during a critical operation in the event dispatch thread
Author: Kevin Dean [TeamB]
Date: Sun, 22 Jan 2006 14:51
Date: Sun, 22 Jan 2006 14:51
31 lines
2265 bytes
2265 bytes
Tony wrote: >1. Display and updates of the progress bar are UI operations > which must take place on a Swing thread. >2. Swing thread will not resume until actionPerformed method > exits. The recommended way to do that in Swing is to spin off the methods in a background thread. You would display the progress bar in a modal dialog with no "Cancel" button (unless you want the ability to cancel the operation) and with the "Close" button in the upper right corner disabled. To update the progress bar, you would post events to the Swing thread. You can find a tutorial here: http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html And some other references on Google: http://www.google.ca/search?q=swing+background+event+progress+bar -- Kevin Dean [TeamB] Dolphin Data Development Ltd. http://www.datadevelopment.com/ NEW WHITEPAPERS Team Development with JBuilder and Borland Enterprise Server Securing Borland Enterprise Server http://www.datadevelopment.com/papers/index.html Please see Borland's newsgroup guidelines at http://info.borland.com/newsgroups/guide.html
Re: progress bar during a critical operation in the event dispatch thread
Author: David C. Browne
Date: Sun, 22 Jan 2006 23:16
Date: Sun, 22 Jan 2006 23:16
47 lines
2099 bytes
2099 bytes
Tony wrote:
> Sample code that explains my problem:
>
> startButton.addActionListener(new Action Listener()
> {
> public void actionPerformed(ActionEvent e)
> {
> method1();
> method2();
> }
> }
> );
>
> method1()
> {
> /*
> I have some long-running operations in this method.
> Is it true that there is no way to display and
> update a progress bar in this method?
> */
> }
>
> method2()
> {
> /*
> This method contains operations that must not
> be performed until method1 is finished
> */
>
> }
>
> My reasoning:
>
> 1. Display and updates of the progress bar are UI operations
> which must take place on a Swing thread.
> 2. Swing thread will not resume until actionPerformed method
> exits.
In addition to Kevin's comments, you may want to have a look at Foxtrot:
http://foxtrot.sourceforge.net/
It may be a better approach than SwingWorker since it uses a synchronous
model, which is what you are asking for with method2().
David Browne
Re: progress bar during a critical operation in the event dispatch thread
Author: Tony
Date: Mon, 23 Jan 2006 19:23
Date: Mon, 23 Jan 2006 19:23
18 lines
1800 bytes
1800 bytes
Thanks a lot for your commends. Kevin, thank you for the pointer. I have always used the method you describe in the past, but in my simplified example (actual case is much more complex) I needed to avoid immediately returning from the eventHandler method. I eventually solved the problem by introducing a useless modal dialog (I used it as a way to re-route the event dispatch thread to allow me to continue posting events to the Swing thread without exiting the original event handler). David, I have not tried using the framework, but by reading the description it absolutely sounds like a very elegant solution to my problem and would allow me to avoid using the ugly modal dialog hack. Thanks a lot for the reference! I greatly appreciate the help! Tony.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads