Ken
Ken

Reputation: 343

Why does this variable keep getting reset every time I activate this timer?

I have two timers. They should both be activated when pressing button1, and both should be stopped when pressing button3. If activated, then every second, the timers add a value of 1 to their respective counters, namely counter and counter1.

If the stop button is pressed, then only the counter of the first timer should be reset to zero, while the counter of the second timer should remain the same.

So if I press start, and after 10 seconds I press stop, then the value of the first counter should be 0 again, while the value of the second counter should be 10.

If I, after this, press start again, and after 10 seconds I press stop again, the value of the first counter should be 0 again, while the value of the second counter should by now be 20.

The problem I'm encountering is that no matter what I do, the value of the second counter keeps resetting if I press stop, so everytime I press stop it becomes 0 again instead of conserving the same value as before the stop button was pressed (so in the example it should be 20).

This is the code (I left some unimportant stuff out, can upload full code if requested):

public class ColoredWordsExperiment {
    Timer timer;
    Timer timer1;
    TimerAction timeraction;
    TimerAction1 timeraction1;
    int counter;
    int counter1;

    ColoredWordsExperiment() {
        button1 = new JButton("Matching");
        button3 = new JButton("Finished");

        buttonHandler = new ButtonHandler(this);
        button1.addActionListener(buttonHandler);
        button3.addActionListener(buttonHandler);

        counter = 0;
        timeraction = new TimerAction(this);
        timer = new Timer(1000, timeraction);
        timer.setInitialDelay(1000);

        counter1 = 0;
        timeraction1 = new TimerAction1(this);
        timer1 = new Timer(1000, timeraction1);
        timer1.setInitialDelay(1000);

        }

    public static void main(String[] arg) {
        new ColoredWordsExperiment();
    }
}

-

class ButtonHandler implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public ButtonHandler(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent e){
        //if button1 pressed
        if (e.getActionCommand().equals("Matching")) {
            coloredWords.timer.start();
            coloredWords.timer1.start();
        //if button3 pressed
    }   else if (e.getActionCommand().equals("Finished")) {
            coloredWords.timer.stop();
            coloredWords.counter = 0;
            coloredWords.timer1.stop();
    }        

-

class TimerAction implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public TimerAction(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        coloredWords.counter++;
    }

}

-

class TimerAction1 implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public TimerAction1(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        coloredWords.counter1++;
    }

}

Upvotes: 1

Views: 336

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Your code with modification works for me:

import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class ColoredWordsExperiment extends JPanel {
   Timer timer;
   Timer timer1;
   TimerAction timeraction;
   TimerAction1 timeraction1;
   private int counter;
   private int counter1;
   private JButton button1;
   private JButton button3;
   private ButtonHandler buttonHandler;
   private JLabel counterLabel = new JLabel("   ");
   private JLabel counter1Label = new JLabel("   ");

   ColoredWordsExperiment() {
      button1 = new JButton("Matching");
      button3 = new JButton("Finished");

      buttonHandler = new ButtonHandler(this);
      button1.addActionListener(buttonHandler);
      button3.addActionListener(buttonHandler);

      counter = 0;
      timeraction = new TimerAction(this);
      timer = new Timer(1000, timeraction);
      timer.setInitialDelay(1000);

      counter1 = 0;
      timeraction1 = new TimerAction1(this);
      timer1 = new Timer(1000, timeraction1);
      timer1.setInitialDelay(1000);

      add(button1);
      add(button3);
      add(new JLabel("Counter:"));
      add(counterLabel);
      add(new JLabel("Counter1:"));
      add(counter1Label);

   }

   public int getCounter() {
      return counter;
   }


   public void setCounter(int counter) {
      this.counter = counter;
      counterLabel.setText(String.valueOf(counter));
   }


   public int getCounter1() {
      return counter1;
   }


   public void setCounter1(int counter1) {
      this.counter1 = counter1;
      counter1Label.setText(String.valueOf(counter1));
   }


   private static void createAndShowGui() {
      ColoredWordsExperiment mainPanel = new ColoredWordsExperiment();

      JFrame frame = new JFrame("Colored Words Experiment");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class ButtonHandler implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public ButtonHandler(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      // if button1 pressed
      if (e.getActionCommand().equals("Matching")) {
         coloredWords.timer.start();
         coloredWords.timer1.start();
         // if button3 pressed
      } else if (e.getActionCommand().equals("Finished")) {
         coloredWords.timer.stop();
         //!! coloredWords.counter = 0;
         coloredWords.setCounter(0); //!! 
         coloredWords.timer1.stop();
      }
   }
}

class TimerAction implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public TimerAction(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent event) {
      coloredWords.setCounter(coloredWords.getCounter() + 1);
      //!! coloredWords.counter++;
   }

}

class TimerAction1 implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public TimerAction1(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent event) {
      //!! coloredWords.counter1++;
      coloredWords.setCounter1(coloredWords.getCounter1() + 1);
   }

}

Confirming for me that your problem lies elsewhere in code not shown.

As a side concern, I do worry about your code's non-OOP direct manipulation of fields. This increases the risk of increased complexity and possible unseen side effect, something that good OOP practices are meant to curtail.

Upvotes: 2

Related Questions