-1

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);

    }
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    You might like to have a read through [Code Conventions for the Java TM Programming Language](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html), it will make it easier for people to read your code and for you to read others – MadProgrammer Nov 09 '14 at 23:07

1 Answers1

2

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.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • thank you for your quick reply but i want the user to type in the time and it stops.... would there be a way to put it on the same button or do i have to do it in another button – java newbie Nov 09 '14 at 23:02
  • @javanewbie: 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. – Hovercraft Full Of Eels Nov 09 '14 at 23:04
  • so i need an independent code to stop with the user input? – java newbie Nov 09 '14 at 23:16
  • @javanewbie: nope, you just need to figure out your stopping condition. Please see additions to my answer above. – Hovercraft Full Of Eels Nov 09 '14 at 23:20
  • okay cool do i put that inside the button or the 'AddOneActionPerformed(evt)'? – java newbie Nov 09 '14 at 23:23
  • @javanewbie: It's all common sense: you put in code that gets the user input and starts your Timer in your button's ActionListener and code that changes the timeLeft variable and that displays the time in your Timer's ActionListener. – Hovercraft Full Of Eels Nov 09 '14 at 23:25