0

What to do to operate the button to stop and start the clock, while I want it when pressed to stop and continue the count with changing of it's label.

Now I reached that the button start the count and change the label after that is doesn't work.

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

public class Ex1 extends JFrame implements Runnable {

    int time = 0;
    JLabel lb1 = new JLabel("Hours:");
    JLabel lb2 = new JLabel("Minutes:");
    JLabel lb3 = new JLabel("Seconds:");
    JTextField hrs = new JTextField(10);
    JTextField mts = new JTextField(10);
    JTextField scd = new JTextField(10);
    JPanel Lcontent = new JPanel();
    Thread t = new Thread(this);
    boolean flag = false;
    JButton stp = new JButton("Start");
    JFrame fr1 = new JFrame("Swing Window");
    Container cp;
    int mnts = 0;
    int hors = 0;

    public Ex1() {
        fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr1.setSize(700, 90);
        fr1.setResizable(true);
        cp = fr1.getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(lb1);
        cp.add(hrs);
        cp.add(lb2);
        cp.add(mts);
        cp.add(lb3);
        cp.add(scd);
        cp.add(stp);

        stp.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if (flag == false) {
                    stp.setText("Stop");
                    t.start();
                }
                flag = true;
                //stp.setText("Start");
                //t.stop();
            }
        });
        fr1.show();
    }

    public void run() {
        while (flag) {
            try {
                Thread.sleep(1000);
                time++;
                scd.setText("" + time);
                if (time > 59) {
                    mnts++;
                    mts.setText("" + mnts);
                    time = 0;
                }
                if (mnts > 59) {
                    hors++;
                    hrs.setText("" + hors);
                    mnts = 0;
                }
            } catch (InterruptedException e) {
            }
        }
    }

    public static void main(String[] args) {
        new Ex1();
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Haroyee
  • 11
  • 5
  • 1
    There's just so many things wrong with this code snippet. For starters, I suggest you read [Lesson: Concurrency in Swing](http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html) – mre Aug 03 '11 at 14:51

1 Answers1

2

Updates to the properties of Swing components should be done on the EDT. The link from above provided by mre will provide more information on this. An easy way to make sure repeating code executes on the EDT is to use a Swing Timer.

See How to Use Swing Timers for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Can you please help me to complete this code ...!? I want to use this way now and learn that things late,, – Haroyee Aug 03 '11 at 15:06
  • 2
    There's a related example [here](http://stackoverflow.com/questions/2576353/stop-a-stopwatch/2576909#2576909). – trashgod Aug 03 '11 at 15:08
  • 2
    I don't know how to complete the code. I would have to spend time looking at the code trying to understand what it does. Since I don't use Threads like that that would take time. I don't spend time learning how to do things the wrong way. I spend time learning how to do things the proper way. – camickr Aug 03 '11 at 15:31