Results 1 to 5 of 5
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default 10 seconds countdown on console

    I wrote this program for a simple count down program. It compiles but nothing happens. Where have i gone wrong. I've never used Timers before:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Timer;
    
    public class timer implements ActionListener{
    	
    	Timer t;
    	private int countdownPeriod = 0;
    	private final int ONE_SECOND = 1000;
    	private void write()
    	{
    		System.out.printf("%s, ", this.countdownPeriod);
    	}
    	
    	public timer(int seconds)
    	{
    		this.countdownPeriod=seconds;
    		t = new Timer(this.ONE_SECOND,this);
    		t.setInitialDelay(0);
    		t.start();
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		if(this.countdownPeriod==0)
    		{
    			t.stop();
    		}else{
    			this.countdownPeriod--;
    			write();
    		}
    	}
    	
    	public static void main(String args[])
    	{
    		timer tom = new timer(10);
    	}
    
    }

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

    Default

    If you want a Timer in a non-GUI application, then don't use a Swing Timer (or javax.swing.Timer), but instead use a util Timer or java.util.Timer.

    Having said that, some of your code would work with a simple Swing GUI:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
    
    public class MyTimer implements ActionListener {
       private JLabel timerLabel = new JLabel("    ");
    
       Timer t;
       private int countdownPeriod = 0;
       private final int ONE_SECOND = 1000;
    
       private void write() {
          //!! System.out.printf("%s, ", this.countdownPeriod);
          timerLabel.setText("" + countdownPeriod);
       }
    
       public MyTimer(int seconds) {
          this.countdownPeriod = seconds;
          timerLabel.setText("" + countdownPeriod);
          t = new Timer(this.ONE_SECOND, this);
          //t.setInitialDelay(0);
          t.start();
          JOptionPane.showMessageDialog(null, timerLabel);
       }
    
       @Override
       public void actionPerformed(ActionEvent arg0) {
          if (this.countdownPeriod == 0) {
             t.stop();
          } else {
             this.countdownPeriod--;
             write();
          }
       }
    
       public static void main(String args[]) {
          MyTimer tom = new MyTimer(10);
       }
    
    }
    Last edited by Fubarable; 04-08-2011 at 07:37 PM.

  3. #3
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    So a swing timer won't work if there isn't as gui?

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

    Default

    Quote Originally Posted by eLancaster View Post
    So a swing timer won't work if there isn't as gui?
    Correct. You need to initiate the main Swing thread and have it persist as in my example above. But the other java.util.Timer is important to learn to use too.

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Thanks! Don't worry - i hope to get atleast a hang of both over the weekend :)

Similar Threads

  1. Replies: 4
    Last Post: 09-09-2010, 12:15 PM
  2. how to countdown on server side
    By BigBear in forum Java Servlet
    Replies: 3
    Last Post: 04-26-2010, 11:33 PM
  3. Replies: 3
    Last Post: 04-05-2010, 02:08 PM
  4. countdown timer, little help with method
    By sidy in forum New To Java
    Replies: 22
    Last Post: 07-19-2008, 01:42 PM
  5. CountDown timer
    By Seema Sharma in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 05:26 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
  •