how to make JDialog show progress?

First of all, yes, I used the search function and I know this problem is all over the board, but as so often, the topics I found don't answer my question.

The Mission: So I want to show my program's user the progress of a function with a progress bar displayed in a custom JDialog along with some other information.

The Solution?: After hours of searching and trying, I stumpled upon http://www.javareference.com/jrexamples/viewexample.jsp?id=77 , which actually works. Problem is, when I try to build it into my app, it doesn't work anymore.

The Problem: So what did I do? First, I put the create dialog and perform computation stuff from the example into a function "progressFunction" instead of doing it right away in the main function. Then I wrote a JFrame that has a button. JFrame knows the programs core class with the main function and progressFunction. Clicking on the button calls the progressFunction from the core class. Function gets executed, JDialog shows, but without its contents, namely a progress bar to show the progress of the function.

The plea for help: As far as I understand, this problem should be solved by putting the execution of the JDialog into a seperate thread, as it is done in the example.

So, does anyone have a clue why this works directly from the programs main function, but not if I take an indirect approach over user interaction with the gui that calls the function?

[1449 byte] By [bluemagiciana] at [2007-11-27 7:50:10]
# 1
Your best bet is the following tutorial:- http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html#monitors
c0demonk3ya at 2007-7-12 19:31:11 > top of Java-index,Desktop,Core GUI APIs...
# 2

Seems to me what that site is telling you to do is backwards.

They are showing a dialog in a separate thread (bad, should be done in the event dispatch thread) and should be spawning a separate thread to do the actual "work", which then updates the status by again through the EDT (although I guess technically main is a separate thread).

In which case you need to look up SwingUtilities.invokeAndWait() or invokeLater()

bsampieria at 2007-7-12 19:31:11 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for your replies.

I knew already about the tutorial, but I need a custom progress JDialog, JProgressMonitor doesn't fulfill my needs.

bsampieri, your answer got me to the solution, but not directly. First of all, I already tried invokeLater(), doesn't work for me.

invokeAndWait() on the other hand can't be called from the EDT, but doing some searching on it got me to the following code: http://www.cs.helsinki.fi/u/vihavain/k03/Java/CoreJavaVolIICh1/ProgressMonitorTestjava.html

which solves my problem. I guess the trick here is the timer that monitors the actual activity. Seems the timer can force the gui to update, while my custom Thread can't.

CU

FLo

bluemagiciana at 2007-7-12 19:31:11 > top of Java-index,Desktop,Core GUI APIs...