0

Hi!

I want to create a stopwatch that will counter seconds starting from 0. This code worked in case of a real time counter (using Calendar). However, now the counter is fixed on 0. Does anybody know what could cause an error? Thanks!

timer = new javax.swing.Timer(1000,
		              new ActionListener() {
		                  public void actionPerformed(ActionEvent e) {
		        			long start = System.currentTimeMillis(); 
		        			long elapsedTimeMillis = System.currentTimeMillis()-start; 
		        			float elapsedTimeSec = elapsedTimeMillis/1000F; 
				            jTextField.setText(Float.toString(elapsedTimeSec));
		                  }
		              });
			  timer.setInitialDelay(0);
			  timer.start();
3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by apines
0

I've updated a code a little and now it's working. However, I don't know how to include MINUTES. Please, help me to finish this task.

elapsedTimeSec = 0;
			  timer = new javax.swing.Timer(1000,
		              new ActionListener() {
		                  public void actionPerformed(ActionEvent e) {

			            jTextField.setText(elapsedTimeSec);
				            elapsedTimeSec++;
		                  }
		              });
			  timer.setInitialDelay(0);
			  timer.start();

Edited by LianaN: n/a

0

You need to think about this problem a bit. Imagine a counter counting the seconds. It starts at 0, then goes to 1, 2, 3, ... etc. How do you know when a minute has elapsed?

0

yes, it was easier than I thought. thanks.

elapsedTimeMin = 0;
		  elapsedTimeSec = 0;
		  timer = new javax.swing.Timer(1000,
	              new ActionListener() {
	                  public void actionPerformed(ActionEvent e) {
	                	  if (elapsedTimeSec < 60) {		                 
			                 elapsedTimeSec++;
	                	  } else {
	                		 elapsedTimeSec = 0;
	                		 elapsedTimeMin++;
	                	  }
	                	  jTextField.setText(elapsedTimeMin + ": " + elapsedTimeSec);	
	                  }
	              });
		  timer.setInitialDelay(0);
		  timer.start();
0

But how could I synchronise the timer with some process that runs during X seconds. I need the timer to count seconds only if the process is running. If the process is terminated, then the timer must stop. Most important is that the process execution time is not known apriori.

My code is shown below. The problem is that the program stops responding for X seconds. How could I synchronise everything?

elapsedTimeMin = 0;
					elapsedTimeSec = 0;
while (myProcess.isExecuting()) {
						  timer = new javax.swing.Timer(1000,
					              new ActionListener() {
					                  public void actionPerformed(ActionEvent e) {
					                	  if (elapsedTimeSec < 60) {		                 
							                 elapsedTimeSec++;
					                	  } else {
					                		 elapsedTimeSec = 0;
					                		 elapsedTimeMin++;
					                	  }
					                	  jTextField.setText(elapsedTimeMin + ": " + elapsedTimeSec);	
					                  }
					              });
						  timer.setInitialDelay(0);
						  timer.start();
}

Edited by LianaN: n/a

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.