In my Java application, I have a timer to keep track of how long someone does not scan a QR code. After 20 seconds, I throw a message telling them to use a manual entry instead of scan the QR code. However, how can I stop the timer once it's started? For example, a user goes to the QR scanning panel. The 20 second timer starts. The user successfully scans a QR code. Now how can I stop the timer? The timer is in my code like this.
Timer timer = new Timer();
timer.schedule( new TimerTask(){
public void run() {
//delay = 20000;
// if the timer has not been stopped, proceed with timer
if(!SpectroClick.stop_timer){
//System.out.println("20 seconds later");
//scanQRPanel.getCameraView().getWebcamPanel().pause();
stopScanning();
// Create the message to display
String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
String message = "<html><div style=\"text-align: center;\">" + text + "</html>";
// Show the confirmation dialog screen, and wait for user input
int i = JOptionPane.showConfirmDialog((Component) null, message,
"Confirm", JOptionPane.OK_CANCEL_OPTION);
// User selected 'OK'
if (i == JOptionPane.OK_OPTION) {
for (SpectroClickEventListener listener : listeners) {
listener.userSelectedOverride();
}
}
// User did not select ok, go to new test
else{
startScanning();
}
}
}
}, delay);
I have a conditional to check if the timer is being used, however is there a way to throw some kind of exception to the timer?