updating a JProgressBar while processing - java

I know the subject has already been seen on many Questions and has been answered, but still, I can't get trough it.
I just want to update a progressBar while extracting some stuff of a large xml file.
I thought it was enough to have the time-consuming loop in a different thread but ?..
All I managed to get is the progressBar either not showed at all, or updated at the end, just before it's closed.
Instanced somewhere near the launch of the application, I have:
public class SomeClass {
private SomeClass () {
myXMLParser reader = new myXMLParser();
CoolStuff fromXml = reader.readTheXml();
}
}
while showing and updating a JDialog with a JProgressBar:
public class LoadingDialog extends JDialog {
private JProgressBar progressBar;
/* ... */
public void progress() {
progressBar.setValue(progressBar.getValue() + 1);
}
}
So I have this myXMLParser:
public class myXMLParser {
private LoadingDialog loadingDialog = new LoadingDialog();
public CoolStuff readTheXml() {
CoolStuff fromXml = new CoolStuff();
while(manyIterations) {
loadingDialog.progress();
fromXml.add(some() + xml() + reading());
}
return fromXml;
}
}
I have seen many things with SwingWorker and using PropertyChange events update the progressBar, but examples are always given all-in-one, with the processing and the progressbar within the same class, and with classes within classes, and since I begin in Java, I wasn't able to translate that to my situation.
Any help ?.. Any (not too obvious) advices ?
Edit: So thanks to btantlinger it worked like that:
public class SomeClass {
private SomeClass () {
myXMLParser reader = new myXMLParser();
new Thread(new Runnable() {
#Override
public void run() {
CoolStuff fromXml = reader.readTheXml();
}
}).start();
}
}
public class LoadingDialog extends JDialog {
private JProgressBar progressBar;
/* ... */
public void progress() {
progressBar.setValue(progressBar.getValue() + 1);
}
}
public class myXMLParser {
private LoadingDialog loadingDialog = new LoadingDialog();
public CoolStuff readTheXml() {
CoolStuff fromXml = new CoolStuff();
while(manyIterations) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
loadingDialog.progress();
}
});
fromXml.add(some() + xml() + reading());
}
return fromXml;
}
}

You MUST update the JProgress bar on the Swing Event Dispatch Thread. You cannot modify Swing components on any other thread.
Your only other alternative would be to set the JProgress bar "indeterminate" before you start your thread where the progress bar will just go back and forth.
E.g
progBar.setIndeterminate(true);
See the SwingWorker javadoc:
http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html
If you don't want to use the SwingWorker, another option is the SwingUtilities.invokeLater method
//inside your long running thread when you want to update a Swing component
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//This will be called on the EDT
progressBar.setValue(progressBar.getValue() + 1);
}
});

In addition to the code provided by #btantlinger, I found after testing that it required an additional line of code in order to update the progress bar on the UI thread while processing. See below.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progressBar.setValue((int)percentage);
//below code to update progress bar while running on thread
progressBar.update(progressBar.getGraphics());
}
});

Related

JProgressBar.setMaximum(int) sometimes doesn't seems to work

I have a JProgressBar that I need to set a new minimum, maximum and progress value each time a task of a queue is done, using the code below:
this.progressBar.setMinimum(minimum);
this.progressBar.setMaximum(maximum);
this.progressBar.setValue(progress);
But, I noticed that sometimes, the setMaximum(int) method doesn't seems to work, because the progress bar still working with the old maximum value.
Then I wrote some test code and ran it:
progressBar.setMaximum(10);
System.out.println(progressBar.getMaximum());
Sometimes it prints 10 as expected, and sometimes prints the old value: anything different from 10.
I spent hours on Google and javadocs, tried to call revalidate() and repaint() on JProgressBar's parent, and nothing. I also tried to call Thread.sleep(10) to wait AWT threads to run another tasks and didn't work.
Any ideas?
EDIT: Provided some more code:
/* Applet that construct the view and init a Thread. */
public class FastApplet extends Applet {
private JProgressBar progressBar;
private JPanel panel;
private Runnable runnable;
#Override
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
#Override
public void run() {
createAndShowGUI();
initThreads();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void createAndShowGUI() {
panel = new JPanel();
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
panel.add(progressBar);
}
private void initThreads() {
runnable = new MyRunnable(progressBar);
Thread thread = new Thread(runnable);
thread.start();
}
}
/* Runnable that update progress and call setMaximum(int) on JProgressBar */
public class MyRunnable {
private int progress;
private final JProgressBar progressBar;
MyRunnable(JProgressBar progressBar) {
this.progressBar = progressBar;
}
#Override
public void run() {
// do the tasks and update the progressBar using progressBar.setValue(progress)
// when done, reset the progress bar with the a new maximum:
defineNewProgressBar(0, 0, newMaximum);
}
public void defineNewProgressBar(int progress, int minimum, int maximum) {
this.progress = progress;
Component component = progressBar.getParent();
JPanel panel = (JPanel) component;
this.progressBar.setMinimum(minimum);
this.progressBar.setMaximum(maximum);
this.progressBar.setValue(progress);
panel.revalidate();
panel.repaint();
}
}
You wrote in your comments that you call setMaximum() not from the EDT. You should call it from the EDT like this:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
progressBar.setMaximum(10);
}
});
If you use Java8, you can do this with using a lambda expression:
SwingUtilities.invokeLater(()->progressBar.setMaximum(10));
You can use SafeProgressBarUpdaterThread :
progressBarUpdater = new SafeProgressBarUpdaterThread(progressBar);
//...
progressBarUpdater.setMaximum(maximum);
progressBarUpdater.setValue(value);
java.awt.EventQueue.invokeLater(progressBarUpdater);

Execute method every second

I want to execude some code every second in android, but I'd like to do is in one thread (main thread). So far I have this:
locationTimer = new Timer("locationTimer", false);
locationTimer.schedule(new LocationCheckerTask(this), 0, 1000);
public class LocationCheckerTask extends TimerTask {
private GeoWatcher watcher;
public LocationCheckerTask(Context context) {
watcher = new GeoWatcher(context);
}
#Override
public void run() {
// funky stuff
}
}
Unfortunately, Timer class runs it's tasks on another thread.
Why I want to do this in a single thread?
Code in run() method will be executing really fast, so I figured I don't need another thread for it. What I want to do is to construct separate threads in run() method based on condition calculated every second. So instead of having child thread constructing another threads, I'd like to do this on the main one.
You can do this with Handler
public class Job implements Runnable{
private Handler handler;
public Job () {
handler = new Handler(Looper.getMainLooper());
loop();
}
#Override
public void run() {
// funky stuff
loop();
}
private void loop() {
handler.postDelayed(this, 1000);
}
}
use runOnUiThread(Runnable) method of Activity to run the task in UI Thread
public class LocationCheckerTask extends TimerTask {
private GeoWatcher watcher;
public LocationCheckerTask(Context context) {
watcher = new GeoWatcher(context);
}
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
// funky stuff
}
});
}
}
the Handler is a perfect candidate for such tasks (dont try to combine TimerTask + runOnUiThread - it is useless as it uses a Handler under the hood)
private Runnable fiveSecondRunnable = new Runnable() {
#Override
public void run() {
if (count5 < 0) {
switchT030Sec();
} else {
tvSec5.setText(""+count5);
Log.v("5sec set", "yes");
count5--;
man.postDelayed(this, 1000);
}
}
};
and start it by calling
man.post(fiveSecondRunnable);

Find existing Jobs in an Eclipse RCP 4 application

I have some long running tasks in my eclipse RCP (e4) application that I implemented using the Job API. The Job is started immediately (before GUI is shown).
private class MyJob extends Job {
#Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("do some long stuff", 4);
doStep1();
monitor.worked(1);
doStep2();
monitor.worked(1);
doStep3();
monitor.done();
}
// methods for the steps follow here
}
Now I want to display a progress bar somewhere in my GUI to show if MyJob is still running.
I have tried this using the IJobManager:
public class MyStatusbar {
private ProgressBar progressBar;
#Inject
UISynchronize sync;
#PostConstruct
public void createControls(Composite parent) {
progressBar = new ProgressBar(parent, SWT.SMOOTH);
progressBar.setBounds(100, 10, 200, 20);
// Setting the progress monitor
IJobManager manager = Job.getJobManager();
final IProgressMonitor p = (IProgressMonitor) new IProgressMonitor() {
#Override
public void worked(final int work) {
sync.syncExec(new Runnable() {
#Override
public void run() {
System.out.println("Worked");
progressBar.setSelection(progressBar.getSelection()
+ work);
}
});
}
// [...]
};
ProgressProvider provider = new ProgressProvider() {
#Override
public IProgressMonitor createMonitor(Job job) {
return p;
}
};
manager.setProgressProvider(provider);
}
}
But my Job is not shown in the status bar. I suppose it has something to do with my starting the job before the GUI is there but I'm not sure.
So I have two questions:
What am I doing wrong?
Is my approach correct or are there better ways to do this?
You do need to create the progress monitor and call IJobManager.setProgressProvider before the Job starts (which is when ProgressProvider.createMonitor is called).
I set up my progress provider in the #PostContextCreate method of my LifeCycle class.

How to setText from a separate Thread in Android

I want to setText from within a Thread.
This is my code of the Thread:
private class GenerateThread implements Runnable {
public void run(){
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
textOut.setText("Initialising...");
});
}
}
in my main code, I use
Thread t = new Thread(new GenerateThread());
t.start();
It does not allow me to setText from within the thread.
Following some posts on internet, I have tried using a handler, but that gave me errors, I think I am double defining Runnable this way.
Handler handler (before main)
private class GenerateThread implements Runnable {
public void run(){
handler.post(new Runnable() {
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
textOut.setText("Initialising...");
});
}
}
How can I setText from within the Thread? Thanks!
besides runOnUiThread there is also View#post(Runnable) which I would prefer here because you don't need ugly looking references to the outer Activity (MyActivity.this.runOnUiThread()).
private class GenerateRunnable implements Runnable {
public void run() {
// this code is executed in a background thread.
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
textOut.post(new Runnable() {
public void run() {
// this code is executed on the UI thread.
textOut.setText("Initialising...");
}
});
}
}
#Override
protected void onResume() {
super.onResume();
new Thread(new GenerateRunnable()).start();
}
Also don't confuse Runnable and Thread. A Runnable is just an ordinary class with a run() method. It can be and often is executed on a new Thread. If you want you can also make GenerateThread a real Thread like so:
private class GenerateThread extends Thread {
#Override
public void run() {
// code here.
}
}
// start somewhere
new GenerateThread().start();
And besides using classic Thread you could also think about using AsyncTask since that is made exactly for tasks that do something long running and need to update the UI afterwards or when there is progress.
One can only update the UI from the UI thread. runOnUiThread will allow you to run an action on the UI thread the next time it executes. You can do something like:
runOnUiThread(new Runnable() {
public void run() {
textOut.setText("Initialising...");
}
});
EDIT:
private class GenerateThread implements Runnable {
public void run(){
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
// Update the UI
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
textOut.setText("Initialising...");
}
});
}
}

how can I reach progressbar from different class?

I have a progressbar in a.java class(in form).I need to reach it from b.java class .
My progressbar name is jprog.(I put it in form from palet)
How can I set public my progressbar? I cant find it in properties page.
my progressbarr in first class. But I want to change its value in second class.
Thanks.
Consider adding a PropertyChangeListener to your first class and using a SwingWorker in your second class. SwingWorker has a method called setProgress() that you can invoke to set the value of your progress bar. You can then override the propertyChange() method in your first class and do something like this:
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
}
An example using a couple of classes, ClassA and ClassB can be as follows:
public ClassA extends JForm implements PropertyChangeListener{
private JProgressBar progressBar;
public ClassA(){
/**
* Your setup for the form
*/
}
/**
* Invoked when task's progress property changes.
*/
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
}
public void someMethod(){
ClassB classB = new ClassB();
classB.addPropertyChangeListener(this);
classB.execute();
}
}
class ClassB extends SwingWorker<Void, Void> {
/*
* Main task. Executed in background thread.
*/
#Override
public Void doInBackground() {
Random random = new Random();
int progress = 0;
//Initialize progress property.
setProgress(0);
//Sleep for at least one second to simulate "startup".
try {
Thread.sleep(1000 + random.nextInt(2000));
} catch (InterruptedException ignore) {}
while (progress < 100) {
//Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {}
//Make random progress.
progress += random.nextInt(10);
setProgress(Math.min(progress, 100));
}
return null;
}
}
You can add to your a class method that will update the progress bar. Then You do not have to touch directlly the progress bar.
If you want to update the progressbar while your need to do it by pressing a Button on ClassA, I still suggest the Sujay's solution, But you need to call your method using the Runnable context. It's important to avoid performing the action within context of the Event Dispatching Thread that might block the updating of progressBar
public void someMethod(){
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// Some stuff
classB.addPropertyChangeListener(this);
classB.execute();
}
});
}

Categories

Resources