Forums Register Login

Help me please ... Timer

+Pie Number of slices to send: Send
Hey there,
I have a class where there is a button on a frame and a timer class. What I'm trying to do is close the frame once the user hasn't click on any of the button(for example 5 seconds).
But at the same time if the user has been clicking ... I want to reset the timer....... I will supply my Timer.class and as well and this little example

Timer class
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class TimerActive extends Thread implements Runnable
{
Thread t;
static String status ="ok";

public TimerActive()
{
t = new Thread (this);
t.start();
}
public void run()
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Interrup Execption" + e);
}
getValade("time is up");
}
public static String getActive()
{
return status;
}
public static String getValade(String str)
{
status = str;
return status;
}
}
And here is my frame
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class PressButton extends Frame implements ActionListener
{
Button bt = new Button("push me");
TimerActive act ;
TimerActive act1 ;
public static void main (String args[])
{
PressButton press = new PressButton();
}
PressButton()
{
setSize(400,400);
setLayout(null);
bt.setBounds( 20,50,60,29);
bt.addActionListener(this);
add(bt);
this.setVisible(true);
act = new TimerActive();

}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bt )
{

if (TimerActive.getActive().equals ("ok") )
{
System.out.println(TimerActive.getActive() );
//TimerActive.t.restart();
}
else
System.out.println(TimerActive.getActive() );
}
}
}
All I'm doing is to see if I'm still within 5 seconds, and t.restart() doesn't work.
+Pie Number of slices to send: Send
sorry I gave out the wrong source ...
Timer class
import java.util.Timer;
import java.util.TimerTask;

public class TimerActive
{
Timer timer = new Timer() ;
int sec = 5;
static String status = "ok";
public TimerActive()
{
timer = new Timer();
timer.schedule(new RemindTask(), 5*1000);
}
class RemindTask extends TimerTask
{
public void run()
{
getValade("time is up");
timer.cancel(); //Terminate the timer thread
}

}
public static String getActive()
{
return status;
}
public static String getValade(String str)
{
status = str;
return status;
}
crispy bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 835 times.
Similar Threads
positioning of components w/o using layout managers
About pop up a window
labels
Label
Button
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:00:41.