Bobby C. Robillard
Bobby C. Robillard

Reputation: 157

Java Timer Program won't update time displayed

I'm creating a simple java timer, but the JPanel which is responsible for displaying the time won't update when the "start" button is clicked. I used a swing timer to update the JPanel but to no avail. Am I using it on the wrong component? Here is my code...

The JFrame(Main Timer Component)

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class theTimer extends JFrame{

    //Initialize fields
    private TimerPanel tp = new TimerPanel();
    private JButton start,stop,reset;
    private Dimension buttonSize = new Dimension(80,30);
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    //Main Method
    public static void main(String[] args){
        theTimer tT = new theTimer();
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    //Constructor
    public theTimer(){
        setLayout(new FlowLayout());
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        add(tp);
        addButtonAction();
        setButtonSize();
        add(start); add(stop); add(reset);
        setTitle("Java Study Timer");
        setVisible(true);
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    //Set the size of the timer buttons
    private void setButtonSize(){
        start.setPreferredSize(buttonSize);
        stop.setPreferredSize(buttonSize);
        reset.setPreferredSize(buttonSize);
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    //Gives the buttons functionality
    private void addButtonAction(){
        start = new JButton("Start");
        stop = new JButton("Stop");
        reset = new JButton("Reset");
        start.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                tp.startTimer();

            }
        });
        stop.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                tp.stopTimer();

            }
        });
        reset.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                tp.resetTimer();
            }
        });
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}

Here is the JPanel class(What the displays the time)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;

public class TimerPanel extends JPanel{
    private int min,sec;
    private String theTime = min + ":" + sec;
    private int width=350, height=300;
    private boolean timerStarted=false;
    private Timer swingTimer = new Timer(900, new ActionListener(){
        public void actionPerformed(ActionEvent event){
            if(sec<60){
                sec++;
                repaint();
            }else{
                min++;
                sec=0;
                repaint();
            }
        } 
    });
    //Constructor
    public TimerPanel(){

    setPreferredSize(new Dimension(350,300));
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    //start the timer
    public void startTimer(){
        swingTimer.start();
    }

    //Stop the timer
    public void stopTimer(){
        swingTimer.stop();
    }

    //reset the timer
    public void resetTimer(){
        sec=0; min=0;
        repaint();
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
    //Paint Method
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.PLAIN, 40));
        g.drawString(theTime, width/2-45, height/2);
        setBackground(Color.BLACK);
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
}

Upvotes: 0

Views: 96

Answers (1)

Sanjeev Saha
Sanjeev Saha

Reputation: 2652

Please insert following statement in TimerPanel.java:

theTime = min + ":" + sec;

before each and every occurrence of repaint(); and see the results.

With above changes, TimerPanel.java is as follows:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;

public class TimerPanel extends JPanel{
    private int min,sec;
    private String theTime = min + ":" + sec;
    private int width=350, height=300;
    private boolean timerStarted=false;
    private Timer swingTimer = new Timer(900, new ActionListener(){
        public void actionPerformed(ActionEvent event){
            if(sec<60){
                sec++;
                theTime = min + ":" + sec;
                repaint();
            }else{
                min++;
                sec=0;
                theTime = min + ":" + sec;
                repaint();
            }
        } 
    });
    //Constructor
    public TimerPanel(){

    setPreferredSize(new Dimension(350,300));
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    //start the timer
    public void startTimer(){
        swingTimer.start();
    }

    //Stop the timer
    public void stopTimer(){
        swingTimer.stop();
    }

    //reset the timer
    public void resetTimer(){
        sec=0; min=0;
        theTime = min + ":" + sec;
        repaint();
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
    //Paint Method
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.PLAIN, 40));
        g.drawString(theTime, width/2-45, height/2);
        setBackground(Color.BLACK);
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------  

}

Also, please remove all the occurrences of tp.updateTime(); from theTimer.java since it is not needed. Hope this helps.

Upvotes: 1

Related Questions