Start New Discussion within our Software Development Community

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.

import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author Mike
 */

public class ExitJoke extends javax.swing.JFrame {

    Timer timer;

    public ExitJoke(int seconds) {
       this.setLocationRelativeTo(null);
       
        timer = new Timer();
        
        timer.schedule(new RemindTask(), seconds * 1000);
    }

    class RemindTask extends TimerTask {

        public void run() {
System.out.println ( "Time's up!" ) ;


            timer.cancel(); //Terminate the timer thread
            System.exit(0);
            

            
        }
    }

    /** Creates new form ExitJoke */
    public ExitJoke() {
        initComponents();
    }
private void btnJokeActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    public static void main(String args[]) {
                               System.out.println ( "About to schedule task." ) ;
         new ExitJoke ( 2 ) ;//Time in seconds.


         System.out.println ( "Task scheduled." ) ;

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ExitJoke().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnJoke;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
}
System.exit(0);

comment this, it closes all windows and ends program
find another solution e.g. setting the status window on the invisible

This article has been dead for over six months. Start a new discussion instead.