Timer - Problem

Reply

Join Date: Oct 2009
Posts: 4
Reputation: Mykd is an unknown quantity at this point 
Solved Threads: 0
Mykd Mykd is offline Offline
Newbie Poster

Timer - Problem

 
0
  #1
27 Days Ago
Hi
The timer works fine when you run it alone, there is a set time befor it exits, 3seconds. But when you open it from the previous window, the timer doesn't start or exit. (The purpose of this is on the previous frame of GUI, the user clicks the exit button, a picture is displayed then quits).
Here's the code.
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3.  
  4. /**
  5.  *
  6.  * @author Mike
  7.  */
  8.  
  9. public class ExitJoke extends javax.swing.JFrame {
  10.  
  11. Timer timer;
  12.  
  13. public ExitJoke(int seconds) {
  14. this.setLocationRelativeTo(null);
  15.  
  16. timer = new Timer();
  17.  
  18. timer.schedule(new RemindTask(), seconds * 1000);
  19. }
  20.  
  21. class RemindTask extends TimerTask {
  22.  
  23. public void run() {
  24. System.out.println ( "Time's up!" ) ;
  25.  
  26.  
  27. timer.cancel(); //Terminate the timer thread
  28. System.exit(0);
  29.  
  30.  
  31.  
  32. }
  33. }
  34.  
  35. /** Creates new form ExitJoke */
  36. public ExitJoke() {
  37. initComponents();
  38. }
  39. private void btnJokeActionPerformed(java.awt.event.ActionEvent evt) {
  40. // TODO add your handling code here:
  41. }
  42.  
  43. public static void main(String args[]) {
  44. System.out.println ( "About to schedule task." ) ;
  45. new ExitJoke ( 2 ) ;//Time in seconds.
  46.  
  47.  
  48. System.out.println ( "Task scheduled." ) ;
  49.  
  50. java.awt.EventQueue.invokeLater(new Runnable() {
  51.  
  52. public void run() {
  53. new ExitJoke().setVisible(true);
  54. }
  55. });
  56. }
  57. // Variables declaration - do not modify
  58. private javax.swing.JButton btnJoke;
  59. private javax.swing.JPanel jPanel1;
  60. // End of variables declaration
  61. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
26 Days Ago
  1. System.exit(0);
comment this, it closes all windows and ends program
find another solution e.g. setting the status window on the invisible
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC