how to stop a timer with 'if'

113 Views Asked by At

hi guys i am new to java... :( i just want my button(start) to start a timer but i want to stop the timer automatically with 'if' so for example... when a user enters a time and the timer get's to the timer it stops... so far my coding is like this...

private void startTimerActionPerformed(java.awt.event.ActionEvent evt) {                                               

        javax.swing.Timer tm = new javax.swing.Timer(100, new ActionListener()
        {
            public void actionPerformed (ActionEvent evt) {
                AddOneActionPerformed(evt);
            }   
        });

        tm.start();

        int getTM,getM,getTS,getS,Secs,tenSec,Mins,tenMin;

        getTM = Integer.parseInt(enterTenMins.getText());
        getM = Integer.parseInt(enterOneMins.getText());
        getTS = Integer.parseInt(enterTenSecs.getText());
        getS = Integer.parseInt(enterOneSecs.getText());

        tenMin = Integer.parseInt(tenMins.getText());
        Mins = Integer.parseInt(oneMins.getText());
        tenSec = Integer.parseInt(tenSecs.getText());
        Secs = Integer.parseInt(oneSecs.getText());
    }                                    

and AddOneActionPerformed(evt) is

private void AddOneActionPerformed(java.awt.event.ActionEvent evt) {                                       
        int dd,Secs,tenSec,Mins,tenMin;

        tenMin = Integer.parseInt(tenMins.getText());
        Mins = Integer.parseInt(oneMins.getText());
        tenSec = Integer.parseInt(tenSecs.getText());
        Secs = Integer.parseInt(oneSecs.getText());
        dd= Integer.parseInt(digitValue.getText());
        dd= dd+1;


        if (dd==10)
            dd = 0;

        if (Secs == 10)
            Secs = 0;

        if (dd==0)
            Secs=Secs +1;

        if (tenSec>=6)
            tenSec = 0;

        if (Secs==10)
            tenSec=tenSec +1;

        if (Mins==10)
            Mins = 0;

        if (tenSec==6)
            Mins=Mins+1;

        if (tenMin>=6)
            tenMin=0;

        if (Mins==10)
            tenMin=tenMin+1;

        String ss = Integer.toString(dd);
        digitValue.setText(ss);

        String ff = Integer.toString(Secs);
        oneSecs.setText(ff);

        String gg = Integer.toString(tenSec);
        tenSecs.setText(gg);

        String hh = Integer.toString(Mins);
        oneMins.setText(hh);

        String jj = Integer.toString(tenMin);
        tenMins.setText(jj);

        showDigitActionPerformed(evt);
        showOneSecsActionPerformed(evt);
        showTenSecsActionPerformed(evt);
        showOneMinsActionPerformed(evt);
        showTenMinsActionPerformed(evt);

    }
1

There are 1 best solutions below

6
Hovercraft Full Of Eels On

You can get the Timer instance from the ActionEvent's getSource() method, and then call stop on it. So,...

// for your Timer's ActionListener
@Override
public void actionPerformed(ActionEvent evt) {
   if (someStoppingConditionIsTrue) {
      Timer timer = (Timer) evt.getSource();
      timer.stop();
   } else {
      // code to call repeatedly
   }
}

Note that you've got two separate issues going on, and should solve them separately. The code above is a way to stop a Timer from within its own ActionListener using some condition. Your other question is how to get user input to allow you to change that condition, and that will involve separate code that is independent of the Timer code above.

Consider in addtion:

  • Use two JSpinners to for the user's to input minutes and seconds.
  • Give your class an int totalTime field.
  • Set this value in your startTimerActionPerformed method.
  • Have the Timer's ActionListener decrement this value based on measured elapsed time using the differences in calls to System.getSystemTime().
  • Calculate get the difference between the totalTime and elapsedTime (it will be in milliseconds), say called timeLeft
  • Calculate your minutes and seconds from timeLeft.
  • Then display these values in a JLabel using a formatted String, say something like String.format("%02d:%02d", minutes, seconds).
  • When the timeLeft == 0, that's when you stop your Timer.