V.David
V.David

Reputation: 75

Reuse a timer timertask Java

Soo created a timer using extending timertask.

label_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                label_1.setVisible(false);
                label_2.setVisible(true);
                timer.purge();
                class MyTimeTask extends TimerTask
        {   
            public void run(){
            genReelNumbers();
            laa++;
           if(laa==50){
            timer.cancel();
            timer.purge();
                laa=0;
            label_1.setVisible(true);
            label_2.setVisible(false);}}}
                timer.purge();
                timer.schedule(new MyTimeTask(), 0, 50);}});

But im getting a error with the timer already canceled! As you can see i already tried to use the purge(), soo it cancels the "canceled" timers (dont know if that does make any sence). I want to use this timer each time that i press on the label! Any ideas?

Upvotes: 1

Views: 1349

Answers (3)

V.David
V.David

Reputation: 75

I followed the @Hovercraft advice and changed to javax.swing.Timer

It turned out like this:

//The variable "taxa" is the amount of times that i want it to do the task
    javax.swing.Timer time1 = new javax.swing.Timer(taxa, new ActionListener() {


            public void actionPerformed(ActionEvent e) {

                genReelNumbers();

            }
            });
//starts the timer
         time1.start();


//New timertask
            TimerTask tt = new TimerTask() {

            @Override
            public void run() {
//stops the timer
            time1.stop();
            label_2.setVisible(false);
            label_1.setVisible(true);
            verificarodas();
            }
            };
            Timer time = new Timer(true);

    // the 2000 is how long i want to do the task's
   //if i changed to 3000 it would take 3 seconds (remember it has to be a value on miliseconds) to do the 15 times, and soo on

            time.schedule(tt, 2000);

Upvotes: -1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

First and foremost, this looks to be a Swing application, and if so, you shouldn't be using java.util.Timer and java.util.TimerTask since Swing is single-threaded, and the two classes above create a new thread or threads to achieve their actions, meaning that important code that should be called on the Swing event thread will not be called on this thread. This this risks causing pernicious intermittent and hard to debug threading exceptions to be thrown. Instead use a javax.swing.Timer. Then to stop this timer, simply call stop() on it, and to restart it, simply call start() on it. For more on this, please read: How To Use Swing Timers.

For example, I'm not 100% sure what you're code is supposed to be doing, but it could look something like:

// warning: code not compile- nor run-tested
label_1.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        label_1.setVisible(false);
        label_2.setVisible(true);

        // assuming a javax.swing.Timer field named timer
        if (timer != null && timer.isRunning()) {
            // if the timer is not null and it's running, stop it:
            timer.stop();
        }

        // TIMER_DELAY is an int constant that specifies the delay between "ticks"
        timer = new Timer(TIMER_DELAY, new ActionListener() {
            @Override  // this method will be called repeatedly, every TIMER_DELAY msecs
            public void actionPerformed(ActionEvent e) {
                genReelNumbers();
                laa++;
                if(laa==50){
                    timer.stop();
                    // timer.purge();
                    laa=0;
                    label_1.setVisible(true);
                    label_2.setVisible(false);
                }
            }
        });
        timer.start();
    }
});

Upvotes: 3

after canceling the timer you have no other choice than creating a new object....

Upvotes: 0

Related Questions