9 Replies - 234 Views - Last Post: 06 June 2014 - 02:54 PM Rate Topic: -----

#1 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • Posts: 55
  • Joined: 21-May 14

Stop watch using timer class isn't working

Posted 06 June 2014 - 08:46 AM

I am trying to make a stopwatch using the timer class and JButtons. When I run my program and I hit start the time stays at zero. What I want my stopwatch to do is when I press start it shows the number of seconds that have passed. The start button will then become disabled and the stop button will be the only button able to be pressed. When you hit stop it should enable only the start and restart buttons. Here is my code. Thanks in advance!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class StopWatchPanel extends JPanel 
{	private final int DELAY = 1000;
	private JButton start;
	private JButton stop;
	private JButton restart;
	private Timer timer;
	public static int time = 0;
	private JLabel label;
	private JPanel panel;
	
	
	public StopWatchPanel()
	{	
		label = new JLabel ("Time: "+ time);
		label.setBackground(Color.yellow);
		
		start = new JButton("Start");
		start.setEnabled(true);
		stop = new JButton ("Stop");
		stop.setEnabled(false);
		restart = new JButton ("Restart");
		restart.setEnabled(false);
	
		ButtonListener button = new ButtonListener();
		TimeListener timeKeeper = new TimeListener();
		
		start.addActionListener(button);
		stop.addActionListener(button);
		restart.addActionListener(button);
		
		timer = new Timer(DELAY,timeKeeper);
		
		
		panel = new JPanel();
		panel.setPreferredSize(new Dimension(200,100));
		panel.setBackground(Color.DARK_GRAY);
		panel.add(restart);
		panel.add(start);
		panel.add(stop);
		panel.add(label);
		add(panel);
		
		
	
	}
	
	
	
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{	
			
			
			
			if (event.getSource()==start)
				stop.setEnabled(true);
				start.setEnabled(false);
				timer.start();
				
			if (event.getSource()==stop)
				start.setEnabled(true);
				restart.setEnabled(true);
				stop.setEnabled(false);
				timer.stop();
			if (event.getSource()==restart)
				timer.start();
				time = 0;
				stop.setEnabled(true);
				start.setEnabled(true);
				restart.setEnabled(false);
		}	
	}	

	private class TimeListener implements ActionListener
	{
		
		public void actionPerformed(ActionEvent event)
		{
			label = new JLabel ("Time: "+ time);
			label.setBackground(Color.yellow);
			time +=1;
			
			
		}
		
		
		
		
		
		
		
		
		
		
		
	}
}




This post has been edited by program0025: 06 June 2014 - 08:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Stop watch using timer class isn't working

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,205
  • Joined: 20-September 08

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 08:57 AM

Your actionPerformed is extremely confused. Firstly, you should not be using it for both buttons and the timer. Also you should be using if and else, not just if
Was This Post Helpful? 1
  • +
  • -

#3 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • Posts: 55
  • Joined: 21-May 14

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 09:09 AM

View Postg00se, on 06 June 2014 - 08:57 AM, said:

Your actionPerformed is extremely confused. Firstly, you should not be using it for both buttons and the timer. Also you should be using if and else, not just if

I edited my post and added a separate listener for the timer.
Was This Post Helpful? 0
  • +
  • -

#4 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 566
  • Joined: 24-April 10

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 09:36 AM

label = new JLabel ("Time: "+ time);
label.setBackground(Color.yellow);
time +=1;

why are you creating a new label each time? just use setText to change the text of the existing one...

This post has been edited by mike73: 06 June 2014 - 09:37 AM

Was This Post Helpful? 1
  • +
  • -

#5 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • Posts: 55
  • Joined: 21-May 14

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 10:25 AM

View Postmike73, on 06 June 2014 - 09:36 AM, said:

label = new JLabel ("Time: "+ time);
label.setBackground(Color.yellow);
time +=1;

why are you creating a new label each time? just use setText to change the text of the existing one...

OK I fixed that however it is still stays at zero when I hit start.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 11:14 AM

Please show us your updated code :)
Was This Post Helpful? 1
  • +
  • -

#7 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • Posts: 55
  • Joined: 21-May 14

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 11:50 AM

View PostCasiOo, on 06 June 2014 - 11:14 AM, said:

Please show us your updated code :)/>

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class StopWatchPanel extends JPanel 
{	private final int DELAY = 1000;
	private JButton start;
	private JButton stop;
	private JButton restart;
	private Timer timer;
	public static int time = 0;
	private JLabel label;
	private JPanel panel;
	
	
	public StopWatchPanel()
	{	
		label = new JLabel ("Time: "+ time);
		label.setBackground(Color.yellow);
		
		start = new JButton("Start");
		start.setEnabled(true);
		stop = new JButton ("Stop");
		stop.setEnabled(false);
		restart = new JButton ("Restart");
		restart.setEnabled(false);
	
		ButtonListener button = new ButtonListener();
		TimeListener timeKeeper = new TimeListener();
		
		start.addActionListener(button);
		stop.addActionListener(button);
		restart.addActionListener(button);
		
		timer = new Timer(DELAY,timeKeeper);
		
		
		panel = new JPanel();
		panel.setPreferredSize(new Dimension(200,100));
		panel.setBackground(Color.DARK_GRAY);
		panel.add(restart);
		panel.add(start);
		panel.add(stop);
		panel.add(label);
		add(panel);
		
		
	
	}
	
	
	
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{	
			
			
			
			if (event.getSource()==start)
				stop.setEnabled(true);
				start.setEnabled(false);
				timer.start();
			
			if (event.getSource()==stop)
				start.setEnabled(true);
				restart.setEnabled(true);
				stop.setEnabled(false);
				timer.stop();
			if (event.getSource()==restart)
				timer.start();
				time = 0;
				stop.setEnabled(true);
				start.setEnabled(true);
				restart.setEnabled(false);
		}	
	}	

	private class TimeListener implements ActionListener
	{
		
		public void actionPerformed(ActionEvent event)
		{
			label.setText("Time: "+ time);
			label.setBackground(Color.yellow);
			time +=1;
			
			
		}
		
		
		
		
		
		
		
		
		
		
		
	}
}



Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 12:25 PM

Look at your ActionPerformed and the if-statements
You are missing brackets
Your code should actually be indented like this
			if (event.getSource()==start)
				stop.setEnabled(true);
			start.setEnabled(false);
			timer.start();
			System.out.println("Start");
			
			if (event.getSource()==stop)
				start.setEnabled(true);
			restart.setEnabled(true);
			stop.setEnabled(false);
			System.out.println("stop");
			timer.stop();
			
			if (event.getSource()==restart)
				timer.start();
			time = 0;
			stop.setEnabled(true);
			start.setEnabled(true);
			restart.setEnabled(false);
			System.out.println("Restart");


Was This Post Helpful? 1
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,205
  • Joined: 20-September 08

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 12:43 PM

And, as i mentioned earlier, your code will be less confusing, less confused and more efficent with else if
Was This Post Helpful? 1
  • +
  • -

#10 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • Posts: 55
  • Joined: 21-May 14

Re: Stop watch using timer class isn't working

Posted 06 June 2014 - 02:54 PM

OK I got it working. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1