I run 2 tasks in this sequence:
- Run first task
- First task is done
- Wait for user to press a button
- Run second task
- Second task is done
- Exit
I made a small, simple GUI to show what is happening. This is what it should look like for each step.
In practice, the 4. GUI doesn't happen at all, it just stays as done while doing the second task and then goes straight to done without the button (5, 6.).
This is my code:
// Setting up the GUI
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
ImageIcon loading = new ImageIcon("loading.gif");
JLabel label = new JLabel("loading... ", loading, JLabel.CENTER);
contentPane.add(label, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Do the first task
for (int i = 0; i < 50000; i++) {
System.out.println(i);
}
// Show when the task is done
label.setIcon(null);
label.setText("done");
// Add the button that must be clicked to continue
JButton startButton = new JButton("check");
startButton.addActionListener(e -> {
contentPane.remove(startButton);
label.setIcon(loading);
label.setText("loading...");
frame.validate();
// Do the second task
for (int i = 0; i < 50000; i++) {
System.out.println(i);
}
// Show when it's done
label.setIcon(null);
label.setText("done");
frame.validate();
});
contentPane.add(startButton, BorderLayout.SOUTH);
frame.validate();
How to I make it so that the GUI updates to loading before running task2?
I've tried to do an inner class, to change GUI components to fields and call another method, nothing seems to work. Any help appreciated.