• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Applet timer

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
like displaying Textfield and label in a applet,I want to display a timer in applet.That timer should work like a stopwatch.like when i clicking a button ,first time the time should start and when i click another time it should stop.how to do this?

Thanks.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a continuation of this work?

You'd need a thread running that periodically wakes up and increments some timing counter. The thread would start if the button is clicked, and be stopped when the button is clicked again.

Stopping the thread can be implemented by setting a boolean variable from the ActionListener, and then checking that variable in the thread.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
without thread,I have tried a program below.
.
Now its just displaying time in milliseconds when i press start button.If want to display it in hh:mm:ss,what should i do?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If "ms" is the time in milliseconds, then

hours = ms / 1000.0 / 60.0 / 60.0

minutes = (ms - hours * 60 * 60 * 1000) / 1000.0 / 60.0

seconds = (ms - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000.0

You'll need to do some appropriate rounding using the Math.floor method, and also think about adding leading zeros if the numbers are smaller than 10. If you're feeling adventurous, you might also have a look at the java.text.SimpleDateFormat class, which makes formatting dates and times much easier.

As an aside, please post formatted code. I'm assuming that you have indented code (if not then you should get in the habit), but in the post all formatting is lost.
[ December 15, 2007: Message edited by: Ulf Dittmer ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
i cant understand the following lines

You'll need to do some appropriate rounding using the Math.floor method, and also think about adding leading zeros if the numbers are smaller than 10. If you're feeling adventurous, you might also have a look at the java.text.SimpleDateFormat class, which makes formatting dates and times much easier.
can you explain in brief
from next time onwards i'l see that my code gets intended....
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You'll need to do some appropriate rounding using the Math.floor method,


Doing divisions gives you a result like "10.42324" or something, when you really need an integer that is rounded down like "10". That's what Math.floor does.

and also think about adding leading zeros if the numbers are smaller than 10.


Say, if the minutes are "9" you still want to show "09" in the output. So you need to check for values smaller than 10 and add an additional "0" in front of them.

If you're feeling adventurous, you might also have a look at the java.text.SimpleDateFormat class


Let's just ignore that. You'll learn more by using the approach I outlined above.
[ December 17, 2007: Message edited by: Ulf Dittmer ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,I've tried the following program.But in applet browser ,when start is clicked i got the output0.0.
.
what is my mistake?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't looked at the details, but two things jump out.

These 3 should be "int", not "double". You need to use Math.floor like I suggested, and then cast to int. Otherwise the math doesn't work out.


You're aware that this will only display the seconds, since everything else will be erased?

I would also slow the timer way down. What good is having it fire every millisecond if the display only cares about seconds? That takes up too many CPU cycles accomplishing nothing.
[ December 17, 2007: Message edited by: Ulf Dittmer ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ December 17, 2007: Message edited by: preethi Ayyappan ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please re-read my previous post. You need to use Math.floor and a cast to int when calculating hours/minutes/seconds, not during display. Plus, you have not addressed the second part of what I wrote.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I would also slow the timer way down. What good is having it fire every millisecond if the display only cares about seconds? That takes up too many CPU cycles accomplishing nothing.


I dont understand this.could you please explain?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have set the timer to fire once every millisecond, while the display code (once you correct the problem I pointed out) only changes its display every second. So the code will run many times without changing anything on the screen. To me, that makes no sense.

By the way, in my previous post, by "second part" I meant the multiple calls to "setText". There should only be a single call that displays hours, minutes and seconds in one go.
[ December 18, 2007: Message edited by: Ulf Dittmer ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello ulf,
Can you show me some sample code regarding this 'setText' which can call more than one variable......
i've tried this...

why this is not working?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why this is not working?


Because it's not doing what I've said repeatedly above - cast the result to an int:
The way to display more than one value is to use string concatenation:

[ December 19, 2007: Message edited by: Ulf Dittmer ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Thank you for your help.I've tried that code.But Now the time is displayed as 0:0:0 ,if i click the start button.whether i have to change the time passed?here is the code what i tried.



[ December 19, 2007: Message edited by: preethi Ayyappan ]
[ December 19, 2007: Message edited by: preethi Ayyappan ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that the timer runs every 100ms you should increase timePassed by 100 on every iteration.

As an aside, a more accurate way to determine time passed would be to store System.currentTimeMillis() when the timer starts, and then to get it again on each iteration in the actionPerformed method, and to subtract the start time. By counting in your own code you're relying on the timer running exactly every 100ms, which is unlikely to be the case.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the inconvinience......can you explain me in brief.


As an aside, a more accurate way to determine time passed would be to store System.currentTimeMillis() when the timer starts, and then to get it again on each iteration in the actionPerformed method, and to subtract the start time. By counting in your own code you're relying on the timer running exactly every 100ms, which is unlikely to be the case.


I've tried upto what i understood.

It shows the error:
t1.getTime(System.currentTimeMillis());
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Cannot invoke getTime(long) on the primitive type long

Thanks.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cannot invoke getTime(long) on the primitive type long


The error message is quite to the point - long does not contain a method called getTime. Where did you get the idea that it does?

You need something like
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.Its working now.
[ December 20, 2007: Message edited by: preethi Ayyappan ]
reply
    Bookmark Topic Watch Topic
  • New Topic