I have problem with the stopwatch i made. For some reason, when i press "start"
it just lags out. Can someone please check the code and tell me whats wrong?
THANK YOU SO MUCH!

Executor

package stopwatchpackage;

public class StopwatchMain {
	
	public static void main(String[]args) {
		new StopwatchMain();
			
	}
	
	public StopwatchMain() {
		new stopwatch();
	}
}

Stopwatch class

/**
* ---------------------------------------------------------
* @author
* Date Created: Sunday September 19 2010*
* Last Update: Sunday September 19 2010*
* 
* 
* 
* ---------------------------------------------------------
**/

package stopwatchpackage;

import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class stopwatch extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// JButtons
	private JButton start;
	private JButton stop;
	private JButton reset;
	
	// JLabel
	private JTextField time;
	
	// JPanel
	private JPanel buttonPanel;
	private JPanel mainpanel = new JPanel();
	// boolean 
	private boolean isRunning = false;
	
	// long
	private long startTime;
	private long stopTime;
	private long Elapsed;
	
	public stopwatch() {
		//House Keeping
		super("Java Stopwatch by Jeel Shah");
		setSize(300,150);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		mainpanel.setLayout(new FlowLayout());
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,3));
		//Labels
		time = new JTextField(5);
		time.setText("0.00");
		
		start = new JButton("Start");
		stop = new JButton("Stop");
		reset = new JButton("Reset");
		
		buttonPanel.add(start);
		buttonPanel.add(stop);
		buttonPanel.add(reset);
		
		time.setLocation(150, 75);
		mainpanel.add(time);
		mainpanel.add(buttonPanel);
		
		add(mainpanel);
		
		start.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						isRunning = true;
						 while (isRunning) {
							    
						      //--- Wait 0.01 seconds, then determine passed time in hundreds of seconds
						      try {
								Thread.sleep(10);
							} catch (InterruptedException e1) {
								e1.printStackTrace();
							}
						      long hs = (System.currentTimeMillis() - startTime) / 10;

						      // While running, display the passed time
						     
						        time.setText( String.format( "%d:%02d.%02d", hs/6000, hs/100%60, hs%100 ));
						    
						  }
				}
			}
		);
		stop.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						isRunning = false;
						stopTime = System.currentTimeMillis();
						Elapsed = (stopTime - startTime)/1000;
						time.setText(""+Elapsed);
					}
				}
		);
		reset.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						time.setText("0.0");
					}
				}
		);
	}
}

Why are you using anonymous functions? Its very unclear. Also your start action listener is wrong I think. It goes into an infinite loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.