I have a message label and a submit button. The submit button will be pressed multiple times, and the action for the each press can take up to a minute.
When the button is pressed, I want to set the message to empty, and after the task is complete, I want to set the message to "Complete".
private void submitActionPerformed(java.awt.event.ActionEvent evt) {
message = "";
updateMessageLabel();
doTheTask();
/* this update is apply to the label after completion */
message = "Complete";
}
Is it possible to update that message label before the submitActionPerformed()
method is run (or in the method), but after the the button is clicked?
Although the Swing concurrency tutorial already contains some very good samples on how to deal with concurrency in Swing, find below an example which
SwingWorker
has a label, which gets updated once the
SwingWorker
is finishedYes you can do this using
SwingWorker
thread, do all the presubmitActionPerformed()
activities like updating the label, in theexecute()
method of thecurrentThread
and calldoTheTask()
as a background job using workerThread
.I suggest you to go through this documentation for reference about SwingWorker Thread