2 Replies - 14780 Views - Last Post: 21 October 2011 - 07:41 AM Rate Topic: -----

#1 mehraan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-October 11

using timer in netbeans java GUI

Posted 21 October 2011 - 07:11 AM


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

public class timerM {
    Timer timer; 
    
    class RemindTask extends TimerTask  {  
         public void run (  )   {  
             System.out.println ( "Time's up!" ) ; 
       timer.cancel (  ) ;  
          }  
      }  
    
     public timerM ( int seconds )   {  
         timer = new Timer (  ) ; 
         timer.schedule ( new RemindTask (  ) , 5*1000 ) ; 
      }  



}





i've got that code in a class, is it correct? and how do i implement and use it in a JFrame to close the jFrame when the timer is up

This post has been edited by mehraan: 21 October 2011 - 07:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: using timer in netbeans java GUI

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • Posts: 2,620
  • Joined: 29-July 11

Re: using timer in netbeans java GUI

Posted 21 October 2011 - 07:39 AM

You have an int parameter in your constructor, but then do nothing with it. As for closing the JFrame, you could call dispose() or System.exit(0).
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,088
  • Joined: 27-December 08

Re: using timer in netbeans java GUI

Posted 21 October 2011 - 07:41 AM

I would just use a Swing Timer in your program, and dispose() your JFrame when the Swing Timer fires an ActionEvent.
class MyFrame extends JFrame{

    private Timer t;

    MyFrame(){
        //other code
 
        t = new Timer(500, new ActionListener(){
            
              public void actionPerformed(ActionEvent e){
                   MyFrame.this.dispose();
              }
        });
    }
}



Also, NetBeans is just a fancy editor. Just make sure you stay away from the GUI Builder, as it produces horrid, unmaintainable code.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1