Results 1 to 6 of 6
  1. #1
    jackwong is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Unhappy Timer not working? help!

    Hey guys.. sorry to disturb again I just finished my stopwatch coding, it just doesn't work.. i don't know why please help ><.. thanks..
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    
    public class iduno extends JFrame {
        
    	private Timer timerSW;
    	private JButton start, reset, stop;
    	int millisec, sec, mins, hrs;
    	JLabel display;
    	public iduno(){
    		super("Hey hoss!");
    		setLayout(new FlowLayout());
    		timerSW = new Timer(100, new thehandler());
    		JButton start = new JButton("start");
    		add(start);
    		JButton reset = new JButton("reset");
    		add(reset);
    		JButton stop = new JButton("stop");
    		add(stop);
    		JLabel display = new JLabel("0:0:0.0");
    		add(display);
    		
    		thehandler handler = new thehandler();
    		start.addActionListener(handler);
    		reset.addActionListener(handler);
    		stop.addActionListener(handler);
    	}
    	
    	private class thehandler implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    		 if(event.getSource()==start){
    		 timerSW.start();
    		 start.setBackground(Color.black);
    		 start.setText("Running");
    		 stop.setBackground(Color.LIGHT_GRAY);
    		 stop.setText("Stop");
    		  }
    	
    		 if(event.getSource()==stop){
    		 timerSW.stop();
    		 start.setBackground(Color.RED);
    		 start.setText("Stopped");
    		 stop.setBackground(Color.LIGHT_GRAY);
    		 stop.setText("Start");
    		  }
    		 if(event.getSource()==reset){
    			 if(timerSW.isRunning()){
    				 timerSW.restart();
    				 }else if(!timerSW.isRunning()){
    					 timerSW.restart();
    					 timerSW.stop();
    					 stop.setBackground(Color.LIGHT_GRAY);
    					 stop.setText("Stop");
    					 start.setBackground(Color.LIGHT_GRAY);
    					 start.setText("start");
    				 }
    		 }
    		 if(timerSW.isRunning()){
    			 millisec++;
    			 if(millisec > 9){
    				 millisec = 0;
    				 sec++;
    			 }
    			 if(sec>59){
    				 sec = 0; 
    				 mins++;
    			 }
    			 if(mins>59){
    				 mins =0;
    				 hrs++;
    			 }
    			 display.setText(hrs + ":" + mins + ":" + sec + "." + millisec);
    		 }
    		 }
    	}
    	
    	
        public static void main(String[]args)
        {
        	iduno buttons = new iduno();
    	      buttons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	      buttons.setSize(550,250);
    	      buttons.setVisible(true);
    		  buttons.setResizable(false);
    		  buttons.setLocationRelativeTo(null);
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: Timer not working? help!

    What do you mean when you say that this doesn't work? Do you see an error? Something else? Please be specific.

    Timer intervals are not exact, so you'd be better off using System.currentTimeMillis() and subtraction to figure out how much time has passed.

    There are 1000 milliseconds in a second, not 10.

    I'd also recommend using standard naming conventions- classes start with an upper case letter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jackwong is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Timer not working? help!

    There's no error at all, but the whole program doesn't work. I wanted to create a stopwatch so when the start button is click the jlabel is suppose to start counting every one second, reset button - reset time, stop button, stop time. Sorry I'm very new to this, it's my first time using timer object

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: Timer not working? help!

    Again, what do you mean by doesn't work? Does it not display at all? Does it countdown incorrectly? Something else?

    Have you stepped through this with a debugger, or at least added some print statements, to figure out what's going on? Does the timer fire at all?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Timer not working? help!

    Also, don't use one ActionListener for such disparate actions. In the very least, your Timer should have its own ActionListener, otherwise you're asking the poor class to do too much.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,421
    Rep Power
    20

    Default Re: Timer not working? help!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. timer not working
    By PRW56 in forum New To Java
    Replies: 4
    Last Post: 03-28-2012, 05:54 PM
  2. Timer And Timer Task
    By Ashish_jain in forum New To Java
    Replies: 3
    Last Post: 06-22-2011, 10:41 AM
  3. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-05-2010, 12:15 AM
  4. Timer stops working :P
    By Addez in forum New To Java
    Replies: 13
    Last Post: 09-21-2010, 09:36 PM
  5. Replies: 0
    Last Post: 04-04-2008, 03: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
  •