There's two things going on here. The first is integer division because symbolsWritten
and totalSymbols
are both int. So you will never get a fractional value from int / int
. It will just jump from 0 to 1. You can make a cast if you want:
bar.setValue((int)Math.round(
100.0 * ((double)symbolsWritten / (double)totalSymbols)
));
(Either that or define them as double to begin with. It's OK to use ++
on a double if the values are in the range of an int.)
The other is the way you are updating the value because it ought to be done on the Event Dispatch Thread. And it depends on how this method gets called. If this is called in an event, you need to be starting a background thread or the updates will not be shown until the process is complete.
@Override
public void actionPerformed(ActionEvent ae) { // some action event
final String someString = "SoMe StRiNg"; // some String
SwingWorker<Void, Void> task = new SwingWorker<Void, Void>() {
@Override
public Void doInBackground() {
writeFileLC(someString.toCharArray());
return (Void)null;
}
@Override
public void done() {
try {
get();
} catch(Exception e) {
e.printStackTrace(System.err);
}
}
};
task.execute();
}
(The funny business in done
is weird but important if you want to catch exceptions that get thrown in doInBackground
. SwingWorker will eat them but will throw an ExecutionException with the eaten exception as its cause if you call get
.)
If you are already running this in a SwingWorker or other Thread, you should be calling these updates on the EDT. Either by using SwingWorker's publish
or by using invokeLater
.
So when you want to update the progress bar inside writeFileLC
:
final int progress = (int)Math.round(
100.0 * ((double)symbolsWritten / (double)totalSymbols)
);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
bar.setValue(progress);
}
});
publish
is a bit less responsive typically than invokeLater
because it combines the results. Personally, I usually use invokeLater to update a progress bar but I guess publish is the way you are "supposed" to do it.
Also see Tasks that Have Interim Results for the tutorial on publish.