JProgressBar not displaying or updating (also on java.gui - sorry)

J

James Gralton

Hi,

I want to display the progress of a task to the user. The task happen in a non gui class of my
application which simply update a currentProgress attribute which I want the Progress Bar class to
check every second and update the bar.

I have the issue that the dialog containing the JProgressBar displays but none of the components are
displayed until the task has completed. Debugging the code I discovered that the timer event which
should fire every second also does not until the task is over.

I am sure I am doing something fundamentally wrong but have searched newsgroups and the net with no
luck. I have also looked at and implemented the Java Sun Progress Bar examples.

Below is the code I have on my main code line:

Code>>>>>>>>>>>>>>>>>>>>>>>

//First we create the ProcessBarDialog but do nothing with it at this stage.

final ProgressBarDialog pbd = new ProgressBarDialog("Paste", false);

SwingWorker bar = new SwingWorker(){
public Object construct() {
pbd.setProgressable(task);
//Display and start the ProgressBarDialog
pbd.startProgress();
return pbd;
}
};
bar.start();


//Actual Task Runs on main line so we don't return until it is complete
java.util.List itemsToRemove = new ArrayList();
for (int i=0; i < pastedObjects.size(); i++) {
if (pastedObjects.get(i) instanceof AdvancedGraph) {
itemsToRemove.add(pastedObjects.get(i));
}
}
pastedObjects.removeAll(itemsToRemove);
.. . .
.. . .
.. . .
CONT...

END CODE>>>>>>>>>>>>>>>>>>>

The ProgressBarDialog class is simple a dialog class which displays a JLabel, JProgressBar and JButton.

pbd.setProgressable(task);

This stored a reference in the Dialog class so it can get the current progress of the task every
time the Timer event fires.

Below is the contents of the pbd.startProgress(); method:

CODE>>>>>>>>>>>>>>>>>>>>

timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {

//Until the task is initalised we can not do anything
if(task != null){
//Get the current progress.
int currentProgress = task.getCurrentProgress();
progressBar.setValue(currentProgress);

if (currentProgress > 0 && progressBar.isIndeterminate()) {
progressBar.setIndeterminate(false);
progressBar.setString(null); //display % string
}

if (task.isDone() ) {//|| cancelled
timer.stop();
progressBar.setValue(progressBar.getMinimum());
progressBar.setString(""); //hide % string
parentFrame.dispose();
//reset the task ready for use again if required.
task.reset();
}
}
}
});

//Display the Progress Bar dialog
displayProgress();

//Set the mode to Indeterminate to start with.
progressBar.setIndeterminate(true);

//Start the timer.
timer.start();

END CODE>>>>>>>>>>>>>>>>>>>>>>>>

The displayProgress() method creates a JDialog and puts the components in the Dialog.

I have tried invokeLater and invokeAndWait in a number of places and the whole thing it really
starting to annoy me.

Thanks for anybodys help.

James
 
J

James Gralton

The problem is it is nerver clear which group a question should go in and if some people only read
one group. My question related to A GUI issue and possible a Thread/EDT issue.
 
A

Andrew Thompson

The problem is it is nerver clear which group a question should go in and if some people only read
one group. My question related to A GUI issue and possible a Thread/EDT issue.

'X-Post'. [ It is different to multi-posting. ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top