user1315906
user1315906

Reputation: 3494

Display a countdown

I need a timer implemented in my application, which will do a countdown from 10 sec - 0 sec. and, display the countdown in a JLabel.

Here's my implementation;

...
Timer t = new Timer(1000, new List());
        t.start();

}

class List implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        int sec = 0;
        label.setText(""+sec);
        // Do a if- condition check to see if the clock has reached to, and then stop

    }

}

I was expecting the JLabel to start counting from 0 - 10 and then stop. But it doesn't. The JLabel set the value 0 and it doesn't get incremented.

UPDATE 1

    t = new Timer(1000, new Listner());
    t.start();


}

class Listner implements ActionListener{
    private int counter = 0;
    @Override
    public void actionPerformed(ActionEvent e) {
        lable.setText(""+ (counter++));

        if (counter == 10)
              t.removeActionListener(this);

    }

}

Upvotes: 1

Views: 13151

Answers (4)

Grcy
Grcy

Reputation: 11

Since java reads the time in milliseconds, it should be 10000 instead of 1000. try your code and see if that works. I had the same problem when I wanted 30 seconds. And instead of writing Timer t = new Timer(30000, new List()); t.start();

I wrote Timer t = new Timer(3000, new List()); t.start();

That made my program to stop every after 3 seconds. I would suggest, you use 10000 instead of 1000.

Remember to do: t.stop() in your List class. Thanks

Upvotes: 1

vels4j
vels4j

Reputation: 11298

A complete example

public class ATimerExample {

    Timer timer;
    int counter = 0;

    public ATimerExample() {
        final JFrame frame = new JFrame("somethgi");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JLabel label = new JLabel("0");
        JPanel panel = new JPanel();
        panel.add(label, BorderLayout.SOUTH);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(String.valueOf(counter));
                counter++;
                if (counter == 10) {
                    //timer.removeActionListener(this);
                      timer.stop();
                }
            }
        });
        timer.start();
    }

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

            @Override
            public void run() {
                new ATimerExample();
            }
        });
    }
}

Upvotes: 3

Jack
Jack

Reputation: 133577

You are not storing nor incrementing secs anywhere so I don't see how it should get updated, try with

Timer timer;

void start() {
  timer = new Timer(1000,new List());
}

class List implements ActionListener {
    private counter = 0;
    @Override
    public void actionPerformed(ActionEvent e) {
        label.setText(""+counter++);

        if (counter == 10)
          timer.removeActionListener(this);
    }
}

Mind that you need to store a reference to the timer somewhere to be able to remove the listener from it once countdown finished.

Upvotes: 4

Fyre
Fyre

Reputation: 1180

Well each time the timer is called it declares the int variable sec to 0. Hence the Label doesnt get updated.

You should declare the sec variable as a global variable and then in the actionPerformed method increment its value each time it is called.

 public int sec = 0;
 class List implements ActionListener{

   @Override
   public void actionPerformed(ActionEvent e) {
        sec++;
        label.setText(""+sec);
        // Do a if- condition check to see if the clock has reached to, and then stop

  }

}

Upvotes: 4

Related Questions