Solved

Using a JProgressBar

Posted on 2004-09-26
4
338 Views
Last Modified: 2008-02-01
hello!

I want to use a JProgressBar in a splash screen of a java program i am writing. The program at the start reads some text files, processes them etc. I want to indicate how much of the processing is done by using a JProgressBar, so the user essentially knows how much is left before the main GUI part of the app shows.

How can i determine the length of the task? How is it usually done, currently, the only way i am aware of tackling this problem is to increment the progress bar by 1 in different locations of the code untill i finally have incremented to 100 (the max value of the JProgressBar i want to use) at the end of the task. To me this seems tedious and very unprofessional, i was wondering how professional java developers would indicate how much of a task is complete using a JProgressBar.

Thx for any tips or help.
0
Comment
Question by:Mr_A_Smith
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154386
If the length of the processing is not known, you'll have to setDeterminate(false) on the JProgressBar
0
 
LVL 5

Accepted Solution

by:
TrekkyLeaper earned 30 total points
ID: 12154387
The way I have done it involves a couple steps:

1. You need to come up with some sort of metric for measuring your progress. This may be something like files loaded / total files, distinct loading operations / total, etc.
2. Once you have that, I would make the splash screen (or a subclass of the progress bar would be even better) an Observer of your loading system.

Observer: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Observer.html
Observable: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Observable.html
Java Almanac: http://javaalmanac.com/egs/java.util/Obs.html

Then, whenever your loading model reaches a new checkpoint, you can do something like:

setChanged();
notifyObservers( new Integer( myProgress ) );
clearChanged();

In the Observer's update method, the Integer passed in will be the second argument. You can take that and adjust the progress bar.

I hope this makes sense.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154389
Or rather

setIndeterminate(true);

0
 
LVL 5

Expert Comment

by:TrekkyLeaper
ID: 12155280
=)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now