How to use timer

How do i use timer with Java.util but without using threads

for (int j = 0; j < button.length; j++)

{

if (button[j] == event.getSource())

{

timer.start () ;

board[j] = num ;

if (checkDouble (i,j) ) {

((JButton)event.getSource()).setText("") ;

board[j] = 0 ;

}

break;

}

}

[388 byte] By [englandRulesa] at [2007-10-2 21:22:25]
# 1
Please stick to one thread. http://forum.java.sun.com/thread.jspa?threadID=740936
Torgila at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 2
can you help me
englandRulesa at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 3

Oh, I see, you are asking about java.util.Timer.

1. Are you sure you don't want to use a Swing Timer? (Your code seems to deal with Swing).

2. If you really want to use java.util.Timer, then read this:

http://java.sun.com/docs/books/tutorial/essential/threads/timer.html

3. The whole idea of a Timer is to let a background thread run some code at certain points in time, so your question becomes rather pointless. However, when you use the Timer API you are not directly exposed to the underlying multithreading. All you have to do is provide one or more TimerTasks, as explained in the tutorial I linked to.

Torgila at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 4
o really, can you tell me how to implement java.swing.Timer into my code?
englandRulesa at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 5
Did you read the tutorial 74philip told you about?
Torgila at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 6
It might also be a good idea to explain what it is you are trying to implement.
Torgila at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 7
I am trying to use a Timer to calculate how long it takes for the user to finish a game.... Do you know what I should do?
englandRulesa at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 8
> It might also be a good idea to explain what it is you are trying to implement.by the look of his posts, it seems like he's trying to get a complete program (in bits and pieces) without having to write one line of code of his own.
Michael_Dunna at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 9

> I am trying to use a Timer to calculate how long it

> takes for the user to finish a game.... Do you know

> what I should do?

If that's all you want to do, you don't need to use a Timer. You'll need to store the time when the game started, something like this:

long startTime = System.currentTimeMillis();

When the game ends, you check the current time again:

long endTime = System.currentTimeMillis();

Subtracting one from the other will give you the time it took to finish the game in milliseconds. From there you can convert it to minutes, seconds, etc.

However, if you want to have some sort of clock that runs while the game is running, showing the user the elapsed time, then you will need a Timer, preferrably of the javax.swing kind.

Torgila at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 10
how do i show the user the elapsed time?
englandRulesa at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 11
Courtesy of 74philip: http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
Torgila at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 12
I didnt understand the Java Api , can you show me an example?
englandRulesa at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 13
There is an example in that tutorial.
Torgila at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 14
sorry i am like a slow learner so i don't get the example in dat tutorial...can you kindly show me how to display a running timer pleasee....your help is appreciated
englandRulesa at 2007-7-14 0:32:43 > top of Java-index,Java Essentials,Java Programming...
# 15

> sorry i am like a slow learner so i don't get the

> example in dat tutorial...

If you think you are a slow learner because you didn't understand that tutorial in the 9 minutes that passed from the time I posted the link to your last reply, then you are being too hard on yourself. Learning takes time.

Torgila at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 16
(...but if you google for "Java Timer clock" you will find code examples that does exactly what you want)
Torgila at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 17
thx!!!
englandRulesa at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 18
create a new thread that waits for 1000ms and then sends an event to update a timer and then goes back to waiting.There are 1,000,000 to do it, but I agree using the swing timer is the best. Why not try using google and doing some of your own work looking for a swing timer example.
Norweeda at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 19
how do i code a stopwatch program without using threads?
englandRulesa at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 20

> how do i code a stopwatch program without using

> threads?

You don't, but again, use a javax.swing.Timer. Then you don't have to worry about the threading stuff yourself.

It should be pretty straightforward now that you know how to use a Timer to write a clock. Start the timer when the start-button is clicked. Stop it when the stop-button is clicked. (You could use a JToggleButton for Start/Stop.)

Torgila at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 21

How the hell would you do it without threads without having your actual program do nothing? I can see why you might want to avoid thread.

1) Threads are scary ooohhhh

2) You're a slow learner.

Come on dude, at some point you should learn these things. Now's a perfect time to spend 3-4 hours reading about threads and how they work. They're really not all that complicated.

Norweeda at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 22
in the line belowlong now = (System.currentTimeMillis())/1000 ;how come i get 1491989 in front of the seconds .... 1 second past... = 149198912 seconds past... = 149198923 seconds past... = 149198934 seconds past... = 14919894
englandRulesa at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 23

Because there's been that many seconds since January 1, 1970. If you'd read the documentation for the method you would have known that.

Okay. Besides tutorials that you can read for yourself, there's also documentation that you can read for yourself. Here's a link to it:

http://java.sun.com/j2se/1.5.0/docs/api/index.html

Every single class and method in the entire standard Java API is in there. You should get in the habit of looking in there, I don't know how people can write Java without it.

DrClapa at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...
# 24
> that you can read for yourself. Read for themselves? Don't be stoopid. The whole point of these fora is so newbs can ask their questions and have others do the research for them.
floundera at 2007-7-21 8:03:11 > top of Java-index,Java Essentials,Java Programming...