Ingram
Ingram

Reputation: 674

How to stop a timer

I have the following action performed method

public void actionPerformed(ActionEvent e) {

    Timer timer = new Timer();
    Object source = e.getSource();

    String stringfromDate = tffromDate.getText();
    String stringtoDate = tftoDate.getText();

    if (source == button) {
        // auto refresh begins
        int delay = 0; // 0 seconds startup delay
        int period = 7000; // x seconds between refreshes
        timer.scheduleAtFixedRate(new TimerTask()
        {
            @Override
            // i still have to truly understand what overide does however
            // netbeans prompted me to put this
            public void run() {

                try {
                    getdata(stringfromDate, stringtoDate);// run get data
                                                            // method
                } catch (IOException | BadLocationException ex) {
                    Logger.getLogger(JavaApplication63.class.getName())
                            .log(Level.SEVERE, null, ex);
                }
            }
        }, delay, period);
    }

    if (source == button1) {
        timer.cancel();

        textarea.setText("");
    }
}

I have 2 buttons on my GUI one called get information(button) and another called clear information (button1). I cant seem to get my clear information(button1) to stop the timer and clear the text area so that a new search can be performed. I just cant seem to get this to stop help appreciated.

Upvotes: 0

Views: 75

Answers (1)

Jan
Jan

Reputation: 13858

Consider these changes to your code. Mainly the code does these things differently:

  • Pull up the declaration of your timer into the class, so that the same timer started before can be cancelled later.

  • only create a new timer if the start-button was pressed.

    //Pulled up for access to make canceable .
    protected Timer timer;
    
    public void actionPerformed(ActionEvent e) {
    
      Object source = e.getSource();
    
      String stringfromDate = tffromDate.getText();
      String stringtoDate = tftoDate.getText();
    
      if (source == button) {
        //Stop existing one before creating new.
        if(timer != null) {
            timer.cancel();
        }
        //Now make new
        timer = new Timer();
    
        // auto refresh begins
        int delay = 0; // 0 seconds startup delay
        int period = 7000; // x seconds between refreshes
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override 
            public void run() {
    
                try {
                    getdata(stringfromDate, stringtoDate);// run get data
                                                            // method
                } catch (IOException | BadLocationException ex) {
                    Logger.getLogger(JavaApplication63.class.getName()).log(Level.SEVERE, null, ex);
    
                }
    
            }
        }, delay, period);
      }
      if (source == button1) {          
        //NULLCHECK
        if(timer != null) {
            timer.cancel();
        }
        textarea.setText("");
      }
    }
    

Upvotes: 3

Related Questions