Engineeringserver.com

A community for computer science students & developers about software development, game development, game design, games, anime preview & reviews and more!


How to create a timertask , a simple reminder application

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
453 views

Here's a demo I wrote to show an example of the usage of a timertask. This demo will show a "reminder" dialog after x seconds.

JAVA:
  1. public class TimerTaskDemoV2 extends JDialog  implements ActionListener {
  2. Timer timer;
  3. JTextArea info = new JTextArea("This is a reminder..\nBy: HappyFace\nwww.engineeringserver.com");
  4. public TimerTaskDemoV2(int seconds) {
  5. timer = new Timer();
  6. timer.schedule(new RemindTask(), seconds*1000);
  7. }
  8. public TimerTaskDemoV2(){
  9. getContentPane().setLayout(null);
  10. info.setEditable(false);
  11. info.setBounds(0,0,200,100);
  12. ok = new JButton("Close");
  13. ok.addActionListener(this);
  14. ok.setBounds(0,100,200,30);
  15. add(info);
  16. add(ok);
  17. setTitle("Reminder");
  18. setDefaultCloseOperation(0);
  19. setSize(207,155);
  20. setResizable(false);
  21. setLocationRelativeTo(null);
  22. setVisible(true);
  23. }
  24. class RemindTask extends TimerTask {
  25. public void run() {
  26. TimerTaskDemoV2 tmp = new  TimerTaskDemoV2();
  27. timer.cancel();
  28. }
  29. }
  30. public void actionPerformed(ActionEvent e) {
  31. if(e.getSource() == ok){
  32. System.exit(0);
  33. }
  34. }
  35. public static void main(String args[]) {
  36. int wait = 5;
  37. System.out.println("Wait "+ wait + " second(s)");
  38. new TimerTaskDemoV2(wait);
  39. }
  40. }


Your Ad Here