Calling the same task more than once

-1

I have the following task:

Timer tempovolta1 = new Timer();
TimerTask tarefavolta1 = new TimerTask() {
    @Override
    public void run() {
        jButton1.setBackground(Color.BLUE);
    }
};

I want to call you several times:

tempo1.schedule(tarefa1, 2000 );

After a while I'll call her and give ERROR :

  

Exception in thread "AWT-EventQueue-0"   java.lang.IllegalStateException: Task already scheduled or canceled

    
asked by anonymous 07.06.2016 / 04:44

1 answer

0

So I realized that you really want or need to use the Swing timer.

Set a constant for example

private static final int TIMER_DELAY = 50;

Your Timer

new javax.swing.Timer(TIMER_DELAY, new ActionListener() {
     public void actionPerformed(ActionEvent e) {
        loopTeste();
     }
  }).start();

And your method

public void loopTeste() {
      button.setLocation(button.getLocation().x + 5, button.getLocation().y);
      getContentPane().repaint();
   }
    
07.06.2016 / 05:17