0

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?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
drake
  • 259
  • 4
  • 17

2 Answers2

2

The question has been already answered here.

The methods you are looking for are "cancel()" and "purge()".

I threw them into your code but haven't had a chance to run it yet:

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();
                    }
               } else{
                  timer.cancel();
                  timer.purge();
               }
            }
         }, delay);
Community
  • 1
  • 1
Gnarlywhale
  • 4,030
  • 2
  • 15
  • 18
  • 1
    IF the question has already been answered, then the question should be closed as a duplicate, but in this case Java.util.Timer isn't the appropriate API for what the op is trying to achieve – MadProgrammer Jul 22 '15 at 22:02
2

You shouldn't be using java.util.Timer for this, but a Swing Timer, as you're violating the single thread rules of Swing with your current approach.

A Swing Timer can be configured for a single run, which means you won't need to worry about stopping it your self

Have a look at Concurrency in Swing and How to use Swing Timers for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366