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));
            }
    }
}
 
    