I am trying to write a class that creates an object of another class. When I run my code, I expect to see a messagebox when the timer reaches the time passed in to the object, but it doesn't. I thought maybe it was because the program hit the end of the main method, so I threw in the thread to keep it executing, but it still didn't give me the message box. Can anyone tell me what I'm doing wrong? Thanks.
import java.util.*;
import java.text.*;
publicclass EventCalendar
{
publicstaticvoid main(String args[])
{
Thread t = new Thread();
DateFormat df = new SimpleDateFormat("MM/dd/yy H:mm");
Date d = new Date();
try {
d = df.parse("6/3/02 12:02");
System.out.println(d.toString());
}
catch (ParseException e){
System.out.println("Unparseable date");
}
AlarmClock a = new AlarmClock(d);
try {
t.sleep(150000);
}
catch (InterruptedException ie) {}
}
}
import java.util.*;
publicclass AlarmClock extends javax.swing.JComponent
{
Timer timer;
TimerTask task = new TimerTask(){
publicvoid run()
{
javax.swing.JOptionPane.showMessageDialog(null,"You have an event due.");
}
};
public AlarmClock(Date timeToSet){
timer = new Timer(true);
timer.schedule(task, timeToSet.getTime());
}
}