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();

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();

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?

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();

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();
}

Then the least you can do is to mark this one as solved.

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.