I want the bar to update every time the method is run.How can I force the UI to update whenever the method is called? You can update the GUI from anywhere, using javax.swing. If you're using Java6.0, you can look into javax.swing.In the Java look and feel, indeterminate progress bars look like this: A visible component to graphically display how much of a total task has completed.
The section Using Indeterminate Mode tells you how to animate a progress bar to show activity before the task's scope is known. Instead, an instance of this class monitors the progress of a task and pops up a dialog if necessary.
See How to Use Progress Monitors for details and an example of using a progress monitor.
One way of indicating work, and perhaps the amount of progress, is to use an animated image.
Another way of indicating work is to set the wait cursor, using the Sometimes you can't immediately determine the length of a long-running task, or the task might stay stuck at the same state of completion for a long time.
You can get the stream's progress monitor with a call to After you see a progress bar and a progress monitor in action, Deciding Whether to Use a Progress Bar or a Progress Monitor can help you figure out which is appropriate for your application.
Here's a picture of a small demo application that uses a progress bar to measure the progress of a task that runs in its own thread: .
I've thought about using threads, but I was taught to use them in separate classes. Updating the progress bar should happen in the Swing Event Dispatch thread. Swing Worker which can help you working with the Event Dispatch thread.
If I do this then, I can't let one thread "go to town" and simultaneously update the GUI in the main thread.
A progress bar that displays a string is likely to be taller than one that doesn't, and, as the demo designers, we've arbitarily decided that this progress bar should display a string only when it's in the default, determinate mode.
However, we want to avoid the layout ugliness that might result if the progress bar changed height when it changed modes.
You can show work without measurable progress by putting the progress bar in A progress bar in indeterminate mode displays animation to indicate that work is occurring.