I have a bot which when started does the following:
boolean botPaused = false;
JButton startButton = new JButton("Start/Resume");
startButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
SwingUtilities.invokeLater(() -> {
botPaused = false;
while (!botPaused) { // infinitely keeps doing this...
advertisementBot.advertise();
}
});
}
});
I want to implement a pause and resume functionality here by changing the botPaused boolean variable. I tried this:
JButton pauseButton = new JButton("Pause");
pauseButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
SwingUtilities.invokeLater(() -> botPaused = true);
}
});
panel.add(pauseButton);
But it does not pause, I think this is because when I press the pause button, the pause action is added to the event thread, but since the original action is never completed, we never reach the pause action.
How to solve this?