|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Timer class ?
im using netbeans and im trying to tell the timer to start only problem is ..
netbeans doesn't recognize two member functions in the Timer library timer.start() and timer.setDelay() Code:
private float converTics(float a)
{
//converts entered miliseconds into seconds
float b;
b = a * 1000;
return b;
}
// set the delay to the specified seconds
timer.setDelay(converTics(speed_tics));
//start the timer
timer.start();
are setDelay or start functions in the library at all ? or do i need to define it myself ? the member functions notify() and schedule are available, can i use those to do my bidding ? |
|
#2
|
|||
|
|||
|
i put the
Code:
clock = new Timer(1, new EventHandler());
....
....
....
....
....
if (a == b)
{
field_one.setText(" some text " );
try
{
clock.wait(10);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
clock.notifyAll();
field_one.setText(" ");
}
though i keep getting a Quote:
error when the conditions are met, not sure what that error means, im trying to have the timer to stop running for 10 secs then clear the status label then continue on till that condition is met again , any suggestions ? |
|
#3
|
|||
|
|||
|
Never seen that exception before.
Instead of clock.wait() could you use Thread.sleep()? Maybe someone knows better, but I think java is telling you that you don't own the Timer class? http://java.sun.com/j2se/1.4.2/docs...ect.html#wait() http://java.sun.com/j2se/1.4.2/docs...tml#sleep(long) |
|
#4
|
|||
|
|||
|
A timer works by sending an event when the time runs out, not by halting program-flow as it seems you think.
You need to handle this event in an appropriate listener (see below). Try this code and report back .Code:
if (a == b)
{
field_one.setText(" some text " );
Timer timer = new Timer(
10*1000, /* to get it as seconds */
new ActionListener() { /* the actionListener which handles the timer */
public void actionPerformed(ActionEvent e) {
field_one.setText(" ");
}
}
);
timer.setRepeats(false); /* we only want to fire one event */
timer.start();
}
Both the ActionListener interface and the ActionEvent class can be found in the java.awt.event package.
__________________
Benjamin Horsleben horsleben.com/benjamin Don't blame malice for what stupidity can explain |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Timer class ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|