Jump to content

I am trying to make a stopwatch with Java. I got the it working where it correctly counts the time and prints the time to the console, but I can not get it to display the time to the actual user interface.(it will be displayed in a JLabel)

import java.awt.*; //imports all utils to make GUI and stuffimport java.awt.event.*;import javax.swing.*;import java.text.DecimalFormat;import java.math.RoundingMode;public class ClassTimerCode extends JFrame implements ActionListener, ItemListener, Runnable{	JTextField t1, t2;//For notes	JLabel l1, l2;//label that says that t1 is for notes	String s1;//displayes string version of the time	double d1 = 0.00;//displays time		public ClassTimerCode()	{		super("'Time spent not learning' timer by Johnny");		setLayout(new FlowLayout());				l1 = new JLabel("Enter notes here:");		add(l1);		t1 = new JTextField("Enter notes here:", 20);		add(t1);		l2 = new JLabel("---0.00---");		add(l2);	}		public void run()	{		try		{			while (1 == 1)//********************************IMPORTANT PART HERE********************************			{				d1 = d1 + 0.01;//update time				DecimalFormat dec = new DecimalFormat("#.##");//set format for truncation				dec.setRoundingMode(RoundingMode.DOWN);//set how to round				s1 = dec.format(d1);//set string to equal truncated double				System.out.println(s1);//display time(string)				l2.setText(s1);//this is not working				Thread.sleep(10);//pause time for 1% of a second or 10 ms			}		}		catch(Exception e)		{					}	}		@Override	public void itemStateChanged(ItemEvent arg0) {		// TODO Auto-generated method stub			}	@Override	public void actionPerformed(ActionEvent arg0) 	{		// TODO Auto-generated method stub	}}

post-636-0-94211700-1377228168.png

person below me is a scrub

Link to comment
https://linustechtips.com/topic/48993-need-help-with-simple-java-stopwatch/
Share on other sites

Link to post
Share on other sites

check if this works

 

line 23 l2 = new JLabel(Double.toString(d1));

 

To display a number or double inside of a label you need to convert it to a string first. Does that make sense?

That works but it only updates the the clock once, it needs to upate constantly just like a real stop-watch. What I should be asking is how to update the thing displayed on a JLabel

person below me is a scrub

Link to post
Share on other sites

That works but it only updates the the clock once, it needs to upate constantly just like a real stop-watch. What I should be asking is how to update the thing displayed on a JLabel

repaint maybe? does system.out.printline(s1) print the correct number. I can't check because it's not compiling on my comp.

 

Man im rusty. I haven't done this in a couple months.

Link to post
Share on other sites

repaint maybe? does system.out.printline(s1) print the correct number. I can't check because it's not compiling on my comp.

 

Man im rusty. I haven't done this in a couple months.

Yes if I do system.out.println(s1) then it does print the correct number, here is the main class, also I do not know what you mean by repaint

import javax.swing.JFrame;public class ClassTimerMainClass {	public static void main(String[] args)	{				ClassTimerCode code = new ClassTimerCode();		Thread th1 = new Thread(new ClassTimerCode());		th1.start();		code.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		code.setSize(1280, 75);		code.setVisible(true);	}}

person below me is a scrub

Link to post
Share on other sites

If you want the stop watch for a website, you might want to use JavaScript over java

 

http://www.codeproject.com/Articles/29330/JavaScript-Stopwatch

 

AJAX JavaScript Stop Watch:

 

http://www.webmasterworld.com/javascript/4062522.htm

Michael Summers

CaseObsidian 900D  MOBORampage IV Extreme CPU:  i7-4960X Ivy Bridge-E 3.6GHz  RAM:: Vengeance Pro 32GB  Boot : RAID 0  840 Pro  512GB  Data:  RAID 10 WD Red  2TB PS: Corsair : AX1200 GPU:  ASUS  GTX-780 Ti

Birthday Gift from my wife - She made me order it and built it :-)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×