3 Replies - 490 Views - Last Post: 06 May 2015 - 08:00 PM Rate Topic: -----

#1 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 55
  • Joined: 25-October 11

Timer is not working

Posted 05 May 2015 - 10:59 PM

Hi i am trying to make taxi meter which shows the current price.Rightnow I have to click startmeter afterevery 1 minute than it's update the new price but I want it to update automatically once the price change after 1 minute instead of me pressing startmeter everytime.This is my 1st time I am using timer class. I am not sure why timer is not working. thanks for your help.

import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.event.ActionListener;
import javax.swing.Timer; 
import java.awt.event.*;

public class taximeter extends JFrame{
    
    JButton start;
    JButton end;
    JLabel total;
    JPanel panel;
    Container pane;
    long star=-1;
    long sto=-1;
      public final static int ONE_SECOND = 1000;
    private final Timer timer;
  
    
    
    
    
    public taximeter(){
        
     super("Taxi Meter");
      
        
      JButton start=new JButton("Start meter");
      JButton stop=new JButton("Stop meter");
        JLabel total=new JLabel("$0.00");
        
     star=System.currentTimeMillis();
     
     
        
        
        panel=new JPanel();
       
        panel.setLayout(null);
        panel.setVisible(true);
        panel.add(start);
        panel.add(stop);
      panel.add(total);
      
      
     
        
        start.setBounds(40, 50, 120, 40);
        
        
        stop.setBounds(40, 125, 120, 40);
     
        total.setBounds(75, 5, 120, 40);
        
    
        
        Container pane=getContentPane();
        
        pane.add(panel);
        pane.setVisible(true);
     
      
        
        GregorianCalendar jc=new GregorianCalendar();
        int lj=jc.getTime().getHours();
        int tj=jc.get(Calendar.HOUR);
        int ne=jc.get(Calendar.MINUTE);
        int jx=jc.get(Calendar.SECOND);
        
       
                        
    

        
        
      
           
          
        stop.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e){
                
                if(e.getSource()==stop){
                    
                    
                    total.setText("$ 5.00");
                    
                    
                }
                    
            }
        }
             
        
        
        
        
        );
        
        
        
        
           start.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        
                        int ld=jc.get(Calendar.MINUTE);
                        int mi=ne-ld;
                        int kd=mi*2;
                        int jm=kd+5;
                        int bc=jc.get(Calendar.HOUR);
                        int dc=(ld-ne)*5;
                        
                   
                    
                        
                        
                       
                        
                        if(e.getSource()==start){
                           
                            
                            
                       
                            
                    sto = System.currentTimeMillis() - star;
                    long lp = sto/(60*1000)% 60;
                    long ts=(lp*3)+3;
                    
                       
                       
                   
                    
                    
                    String lq=Long.toString(ts);
                   
                    
		
                        total.setText(lq);
                        total.repaint();
                        
                        
                             
                        panel.setVisible(true);
                        

                       
                        
                   }     
                        
                            
                        }
                        
                        
                        
                    
                    
                    
                    
                });
           
           
           
        
    
    
      

timer = new Timer(ONE_SECOND, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
	

        if (evt.equals(start)) {
            
            timer.start();
            
           
        }
    }    
});





    
    
        
    }
 
 
    
 

   
    public static void main(String[] args) {
        
      
  taximeter mw=new taximeter();
  mw.setVisible(true);
  mw.setSize(200, 200);
  
  


  

  
    

                
        
      
    }
        
        
     
    }
    




Is This A Good Question/Topic? 0
  • +

Replies To: Timer is not working

#2 Atspulgs  Icon User is offline

  • D.I.C Addict

Reputation: 89
  • View blog
  • Posts: 507
  • Joined: 29-July 09

Re: Timer is not working

Posted 06 May 2015 - 02:04 AM

You're never running the timer:

timer = new Timer(ONE_SECOND, new ActionListener() {
    public void actionPerformed(ActionEvent evt) { 
        if (evt.equals(start)) {
            timer.start();
        }
    }   
});



The action listener you create here is meant to be triggered for each timer interval. Your timer is never started.
Something like this will keep printing the current timestamp every second.
(timer = new Timer(ONE_SECOND, new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
      System.out.println(evt.getWhen());
   }
})).start();



You can setup the timer and start it when you press the button, but you will need to do that in the ActionListener that has been added to the button rather than the Timers one.
Was This Post Helpful? 0
  • +
  • -

#3 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 55
  • Joined: 25-October 11

Re: Timer is not working

Posted 06 May 2015 - 07:35 PM

Thanks I setup timer and it's working fine. The only problem I am facing is that once I run the program the meter start running and when I press the "start meter" button meter don't start from beginning. It show me the price since I run this program. I want it to start when I press "start meter" button. Thanks.
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.event.ActionListener;
import javax.swing.Timer; 
import java.awt.event.*;

public class taximeter extends JFrame{
    
    JButton start;
    JButton stopp;
    JLabel total;
    JPanel panel;
    Container pane;
    long star=-1;
    long sto=-1;
      public final static int ONE_SECOND = 1000;
    private final Timer timer;
  
    
    
    
    
    public taximeter(){
        
     super("Taxi Meter");
      
        
      JButton start=new JButton("Start meter");
      JButton stopp=new JButton("Stop meter");
        JLabel total=new JLabel("$0.00");
        
     star=System.currentTimeMillis();
     
     
        
        
        panel=new JPanel();
       
        panel.setLayout(null);
        panel.setVisible(true);
        panel.add(start);
        panel.add(stopp);
      panel.add(total);
      
      
     
        
        start.setBounds(40, 50, 120, 40);
        
        
        stopp.setBounds(40, 125, 120, 40);
     
        total.setBounds(75, 5, 120, 40);
        
    
        
        Container pane=getContentPane();
        
        pane.add(panel);
        pane.setVisible(true);
     
      
        
        GregorianCalendar jc=new GregorianCalendar();
        int lj=jc.getTime().getHours();
        int tj=jc.get(Calendar.HOUR);
        int ne=jc.get(Calendar.MINUTE);
        int jx=jc.get(Calendar.SECOND);
        
       
                        
    

        
        
      
           
           
 
    
        
        stopp.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e){
                
                if(e.getSource()==stopp){
                    
                    
                    timer.stop();
                    
                    
                }
                    
          }
        }
             
        
        
        
        
        );
        
        
        
        
           start.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        
                        int ld=jc.get(Calendar.MINUTE);
                        int mi=ne-ld;
                        int kd=mi*2;
                        int jm=kd+5;
                        int bc=jc.get(Calendar.HOUR);
                        int dc=(ld-ne)*5;
                        
                   
                    
                        
                        
                       
                        
                        if(e.getSource()==start){
                           
                            
               
                       
                  
                           timer.start();

                       
                        
                   }     
                        
                            
                        }
                        
                        
                        
                    
                    
                    
                    
                });
           
           
           
        
    
    
      

timer = new Timer(ONE_SECOND, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
	
 int ld=jc.get(Calendar.MINUTE);
                        int mi=ne-ld;
                        int kd=mi*2;
                        int jm=kd+5;
                        int bc=jc.get(Calendar.HOUR);
                        int dc=(ld-ne)*5;
                        
                   
                    
                        
                        
                       
                        
                   
                           
                            
                            
                       
                            
                    sto = System.currentTimeMillis() - star;
                    long lp = sto/(60*1000)% 60;
                    long ts=(lp*3)+3;
                    
                    
         
                    String lq=Long.toString(ts);
                  
                        
                    
                   
                    
		
                        total.setText(lq);
   //  System.out.println(evt.getWhen());
     //String lq=Long.toString(evt.getWhen());
     //total.setText(lq);
                        
                    
    
    }    
});   
    }
 
 
    
 

   
    public static void main(String[] args) {
        
      
  taximeter mw=new taximeter();
  mw.setVisible(true);
  mw.setSize(200, 200);}
        
        
     
    }
    



Was This Post Helpful? 0
  • +
  • -

#4 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Timer is not working

Posted 06 May 2015 - 08:00 PM

You need to reset the timer in the stop method. Or add another user interfacr that allows the user to choose between pausing the meter or resetting it.

        stopp.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e){
                
                if(e.getSource()==stopp){
                    
                    
                    timer.stop();
                    
                   //add reset code here
                    
                }
                    
          }
        }
         


This post has been edited by jphoc13: 06 May 2015 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1