Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Need help with TIMER reset code

    Hey guys I need help with this timer I'm doing. I have it working counting down from 35:00mins to 00:00, now I want it to stay that way until I click reset, Then it should display 35:00 mins on the clock again and when I click start again it should just start counting down until it reaches 00:00.

    I'm not sure on how I have to do this at all. I did make a reset button with code like this.

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
    {
    jLabel1.setText("35:00");
    }

    all it does is make sets the display on the clock to 35:00 but when I click start again it starts counting down in 2's and 3's. Not sure how to fix this.

    This is the code of my timer with start button


    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    for(int ss=0; ss<1; ss++) {
    // Change the set name

    // Setup the counter to begin the countdown
    counter = *60;
    timer = new Timer(1000, startCycle());
    timer.start();
    }


    }
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Countdown().setVisible(true);
    }
    });
    }
    private Action startCycle() {
    return new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
    new ClockTask(null).execute();
    }
    };
    }
    class ClockTask extends SwingWorker<Void, String> {
    private Callback callback;
    private Object jLblTimer;
    public ClockTask(Callback callback) {
    this.callback = callback;
    }
    @Override
    protected Void doInBackground() throws Exception {
    if (counter >= 0) {
    int millis = counter*1000;
    String time = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millis),
    TimeUnit.MILLISECONDS.toSeconds(millis) -
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.t oMinutes(millis))
    );
    publish(time); // call publish which will call process() method in EDT
    counter--;
    } else {
    // this.callback.call();
    this.callback.notify();
    timer.stop();
    }
    return(null);

    }

    @Override
    protected void process(List<String> times) {
    String lastTime = times.get(times.size()-1);
    jLabel1.setText(lastTime); // just update ui with lastTimer, ohters are ignorable
    }
    @Override
    protected void done() {
    super.done();
    } // TODO add your handling code here:[/COLOR]
    }




    }
    Last edited by elbert.dewet@yahoo.com; 06-13-2012 at 12:57 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with TIMER reset code

    That's a lot of difficult to read unformatted code. Please have a look at the forum FAQ including how to use [code] [/code] tags, and then edit this post.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Need help with TIMER reset code

    Why did you highlight a bunch of it in bright green? On a white background that's pretty much impossible to read.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Urgently need a countdown timer code...
    By BZwap in forum New To Java
    Replies: 5
    Last Post: 05-08-2011, 06:51 AM
  2. Timer java code does not work
    By mariapatrawala in forum New To Java
    Replies: 10
    Last Post: 12-12-2010, 10:34 AM
  3. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  4. Quit Button, Load new Frame, Timer Code
    By IHateNetbeans in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 03:58 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •