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;
}
}
Please stick to one thread. http://forum.java.sun.com/thread.jspa?threadID=740936
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.
o really, can you tell me how to implement java.swing.Timer into my code?
Did you read the tutorial 74philip told you about?
It might also be a good idea to explain what it is you are trying to implement.
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?
> 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.
> 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.
how do i show the user the elapsed time?
Courtesy of 74philip: http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
I didnt understand the Java Api , can you show me an example?
There is an example in that tutorial.
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
> 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.
(...but if you google for "Java Timer clock" you will find code examples that does exactly what you want)
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.
how do i code a stopwatch program without using threads?
> 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.)
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.
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
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.
> 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.