This is my first time working with threads so I apologize if the answer to this is simple. I have a GUI and when the user clicks a button, I want it to update the text to display the fact that a task had been completed, and a few seconds later return to its normal state. I have the following code:
if (e.getActionCommand().equals("Empty all files.")) {
// LiveDataParser.stop();
// DatabaseSorter.stop();
DBS.setBackground(Color.GRAY);
PLD.setBackground(Color.GRAY);
FLF.setBackground(Color.GREEN);
try {
FileFlusher.main(arr);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Reached");
CTKS.setText("Just finished emptying all relevant files.");
CTKP.setText("Just finished emptying all relevant files.");
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// try {
// TimeUnit.SECONDS.sleep(1);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
//
CTKS.setText("Currently Doing Nothing");
CTKP.setText("Currently Doing Nothing");
}
Here my hope is for the tasks to happen in this order, Change button to green, Update text to emptying files, Wait X amount of time, Update text to doing nothing. I have a S.O.P command in there to track progress which is where things get weird IMO. The program will run and print out “reached” twice into the console while waiting the alloted time to wait. Once Reached has been printed twice and the time has completed 1 more loop, the program will jump to the very end and set the text to doing nothing.
I’ve tried using 2 different types of sleep as seen in the program as well as moving the position of the code around. This has changed nothing unfortunately.
1