First, I need to inform you that I am trying my hardest to learn how to code in Java. Its been a little bit difficult, but, I do believe I have it. I have submitted a couple question in the past in regards to SwingWorkers and the like. Each of which I thought I had it, but come to find out that I still had learning to do. Hopefully this time, is not one of those times.
With that said, please let me know of anything you see that isn't to standard, or, could lead to problems in the future.
Now for the question!
I have constructed a JFrame, of which, loads a couple things before it allows the user to proceed to another page, press buttons, or anything else. Once it loads the data it will then unlock the JFrame to allow the user to interact with the data.
The problem is, (which isn't really a problem, just a clarification) I need to execute another task while the user can interact with the JFrame in such a way it doesn't bother them, but, will update the JFrame based on the results it found. An example might be a version check. Depending if the version is out of date or not, notify user.
Example Sudo Code
protected void startJFrame() {
JFrame myFrame = new JFrame();//Starts with disable/invisible components. Preventing the user from doing to much before allowed.
SwingWorker<Void, Progress> loadingWorker = new SwingWorker<Void, Progress>() {
@Override
protected Void doInBackground() throws Exception {
publish(new Progress(0,"Loading This")); // Set Percent
loadingTasks.loadThis(); // Do Work!!
publish(new Progress(25,"Loading That")); // Set Percent
loadingTasks.loadThat(); // Do Work!!
publish(new Progress(50,"Loading More")); // Set Percent
loadingTasks.loadMore(); // Do Work!!
publish(new Progress(75,"Loading Last")); // Set Percent
loadingTasks.loadLast(); // Do Work!!
publish(new Progress(100,"Loading Complete"));// Set Percent
return null;
}
@Override
protected void process(List<Progress> ProgressList) {
for (Progress p : ProgressList) {
System.out.println(p.getInt() + "% " + p.getString()); //Show user percent and what its doing.
}
}
@Override
protected void done() {
try {
get();
loadingTasks.WrapUp();//Set Variables or other small stuff.
myFrame.userAllowed();//Lets the user interact with the whole JFrame.
SwingWorker<Void, Void> secondWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
versionCheck.makeItSo();// Do Work!!
return null;
}
@Override
protected void done() {
try {
get();
versionCheck.wrapUp();//Set Variables or other small stuff.
myFrame.showLabel();//Show a label with specific info.
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
};
secondWorker.execute();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
};
loadingWorker.execute();
}
Off topic question involving this code.
I am concerned about creating to many objects and not disposing of them, just to pass multiple variables. Specifically the Progress objects being created in the first doInBackground method.
Is it considered OK to do this? Will it auto dispose the Progress Objects automatically? If not, how would i go about disposing them after I'm done with them?
See Question&Answers more detail:
os