Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:23:10+00:00 2026-06-16T02:23:10+00:00

I am trying to make a stopwatch using swing, but it is not working.

  • 0

I am trying to make a stopwatch using swing, but it is not working. Here is my code. The Jlabel clock is always displaying -1, which should only happen if it is stopped. Am I using the invokelater properly?

import javax.swing.*;

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



public class sidePanel extends JApplet implements ActionListener{
    JPanel pane;
    JLabel clock;
    JButton toggle;

    Timer timer;
    StopWatch stopWatch;


    public void init()
    {
        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

        clock = new JLabel("00:00");

        toggle = new JButton("Start/Stop");
        toggle.addActionListener(this);

        pane.add(clock);
        pane.add(toggle);


        timer = new Timer(500, this);
        timer.setRepeats(true);

        stopWatch = new StopWatch();



        add(pane);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == toggle)
        {


            if(timer.isRunning())
            {
                stopWatch.endTime = System.currentTimeMillis();
                timer.stop();
            }
            else
            {
                stopWatch.startTime = System.currentTimeMillis();
                timer.start();
            }
        }

        if(e.getSource() == timer)
        {
            long time = stopWatch.getElapsedTime();
            sidePanel.this.clock.setText(String.valueOf(time));
        }
    }




    private class StopWatch{

        private long startTime =0;
        private long endTime =0;
        public boolean isRunning = false;


        public void start(){
            startTime = System.currentTimeMillis();
            isRunning = true;
        }

        public void end(){
            endTime = System.currentTimeMillis();
            isRunning = false;
        }

        public long getElapsedTime()
        {
            long currentTime = System.currentTimeMillis();
            if(isRunning)
                return (currentTime - startTime)/1000;
            else
                return -1;
        }

    }






}

Working code

import javax.swing.*;

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



public class sidePanel extends JApplet implements ActionListener{
    JPanel pane;
    JLabel clock;
    JButton toggle;

    Timer timer;
    //StopWatch stopWatch;

    boolean pressed = false;

    long startTime =0;
    long endTime =0;


    public void init()
    {
        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

        clock = new JLabel("00:00");

        toggle = new JButton("Start/Stop");
        toggle.addActionListener(this);

        pane.add(clock);
        pane.add(toggle);


        timer = new Timer(500, this);
        timer.setRepeats(true);

        //stopWatch = new StopWatch();



        add(pane);

    }

    long cur;
    long end;
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == toggle)
        {

            if(!pressed)
            {
                timer.start();
                startTime = System.currentTimeMillis();
                pressed = true;
            }
            else
            {
                timer.stop();

                pressed = false;
            }
        }



            if(timer.isRunning())
            {
                endTime = System.currentTimeMillis();
                clock.setText(String.valueOf((endTime-startTime)/1000));

            }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:23:11+00:00Added an answer on June 16, 2026 at 2:23 am

    Your StopWatch class run once and then terminates…

    public void run() {
        // Start here
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run() {
                long time = getElapsedTime();
                sidePanel.this.clock.setText(String.valueOf(time));
            }
        });
        // End here...
    }
    

    A thread will terminate when it exists it’s run method, in this case, your StopWatch‘s run method.

    What you need to do to is maintain a loop until the isRunning becomes false

    public void run() {
        while (isRunning) {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run() {
                    long time = getElapsedTime();
                    sidePanel.this.clock.setText(String.valueOf(time));
                }
            });
            // Because we really don't want to bombboard the Event dispatching thread
            // With lots of updates, which probably won't get rendered any way,
            // We put in a small delay...
    
            // This day represents "about" a second accuracy...
            try {
                Thread.sleep(500);
            } catch (Exception exp) {
            }
        }
    }
    

    It would much simpler to use a javax.swing.Timer though…

    private Timer timer;
    public void init()
    {
        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    
        clock = new JLabel("00:00");
    
        toggle = new JButton("Start/Stop");
        toggle.addActionListener(this);
    
        pane.add(clock);
        pane.add(toggle);
    
        timer = new Timer(500, new ActionListener() { 
            public void actionPerformed(ActionEvent evt) {
                long time = getElapsedTime();
                sidePanel.this.clock.setText(String.valueOf(time));        
            }
        });
        timer.setRepeats(true);
        add(pane);
    
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == toggle)
        {
            if(timer.isRunning())
            {
                endTime = System.currentTimeMillis();
                timer.stop();
            }
            else
            {
                startTime = System.currentTimeMillis();
                timer.start();
            }
        }
    }
    

    You can then strip out the functionality from you StopWatch (ie the getElapsedTime())

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a small countdown timer in my app but it's not working.
I'm trying to make a stopwatch & countdown app for WindowPhone 7 using Silverlight
I'm trying to make a simple stopwatch but it just doesn't work.. The app
I'm trying to make a stopwatch program, which will write czas to file.txt .
I'm trying make this code which is from a Dictionary of Arrays (CODE) from
Trying to make a multilingual installer - the process is working in general, but
I'm trying make some stuff in jQuery using ASP.NET. But the ID from runat=server
I am fairly new to iOS development and trying make a simple app which
im trying make one replace in string from a array but this dont work
I'm trying make a report which contains some prices on every row, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.