I know that the subject has already been seen in many Questions and an answer has been given, but still I cannot miss it.
I just want to update the progressBar by extracting some things from a large xml file. I thought that this was enough to have a long cycle in another thread, but? .. All I managed to get was progress, which either was not shown at all or was updated at the end, shortly before its closure.
Once close to launching an application, I:
public class SomeClass { private SomeClass () { myXMLParser reader = new myXMLParser(); CoolStuff fromXml = reader.readTheXml(); } }
when showing and updating JDialog using 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 saw a lot of things with SwingWorker and using PropertyChange events updating the progressBar, but examples are always given all-in-one with processing and progress bar within the same class and with classes inside the classes, and since I start in Java, I could not translate this into your situation.
Any help? .. Any (not too obvious) tips?
Edit: Thus, thanks to btantlinger, it worked as follows:
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; } }