for this; i want the “Continuous” button to start rollthedice(), and the “Stop” button to stop it, but then when i hit the “Continuous” button again i want it to start rollthedice() again, back and forth
scheduler = new ScheduledThreadPoolExecutor(1);
ScheduledFuture now = null;
Runnable runner = new Runnable(){
public void run()
{
rollthedice();
}
};
if(e.getSource()==continuous)
{
now = scheduler.scheduleAtFixedRate(runner, 0, 500, TimeUnit.MILLISECONDS);
}
if(e.getSource()==stop)
{
now.cancel(true);
}
what you are missing is this Assignment to
ScheduledFuturenowThen you can call
now.cancel(true);Just move below declarations at class level i.e. you can move above your
actionPerformedmethod. It works fine after that.