alexeidebono
alexeidebono

Reputation: 99

Stopping a thread by a swing button

I made a simple program which just tracks the co ordinates of the mouse cursor - it works fine, all I want do is start / stop it with a button.

So far the button starts the thread well, what is the best way which I can safely stop the thread? This is because I may add a facility to write the coordinates to a text file in the future.

Do I make a boolean which makes the thread run only when it is true or something like that? Because I tried to edit the trigger boolean but it had no affect what so ever.

The code as it is :

The class running the thread

public class tester {

static int startTime = (int) System.currentTimeMillis();
static boolean trigger = false;
static JLabel label = new JLabel();
static JLabel status = new JLabel();
static mouseLocate msl = new mouseLocate();
static JButton startButton = new JButton("Begin tracking");
static JButton stopButton = new JButton("Stop Tracker");
static Thread myThread = new Thread(new mouseLocate());

public static void main(String[] args) {
    JFrame frame = new JFrame("Mouse Grabber");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 100);
    JPanel panel = new JPanel();
    frame.add(panel);


    panel.add(startButton);
    startButton.addActionListener(new startAction());

    panel.add(label);
    panel.add(status);
}

static class startAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        try {

            if (trigger == false) {
                trigger = true;
                msl.setTrigger(trigger);
                //label.setText("Trigger Active" + msl.isTrigger());
                startButton.setText("Continue Tracking");
            } else if (trigger == true) {
                trigger = false;
                //msl.setTrigger(trigger);
                label.setText("Trigger Inactive");
                startButton.setText("Stop Tracking");
            }
        } catch (Exception exp) {
            System.out.println("EXCEPTION CAUGHT " + e);
        }



        //RUN 
        myThread.start();


    }
}

The mouse locate class:

public class mouseLocate implements Runnable {

private boolean trigger = false;
private int startTime = (int) System.currentTimeMillis();
static String status = "";

public void run() {
    int x, y;
    while (mouseGrabber.trigger = true) {

        try {
            PointerInfo mouseLocation = MouseInfo.getPointerInfo();
            x = mouseLocation.getLocation().x;
            y = mouseLocation.getLocation().y;
            System.out.println("X:" + x + " Y:" + y + "        Elapsed time:  "
                    + (((int) System.currentTimeMillis() - startTime) / 100));

        } catch (Exception e) {
            System.out.println("Exception caught : " + e);
        }
    }

}

public int getStartTime() {
    return startTime;
}

public void setStartTime(int startTime) {
    this.startTime = startTime;
}

public boolean isTrigger() {
    return trigger;
}

public void setTrigger(boolean trigger) {
    this.trigger = trigger;
}

public static String getStatus() {
    return status;
}

public static void setStatus(String status) {
    mouseLocate.status = status;
}

}

Thank you for any help I really appreciate it.

Upvotes: 2

Views: 4085

Answers (2)

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

    public volatile boolean isShutingDown;   



public void run() {
   while(!isShutingDown) {

   }
}

call shutDown() method, when you need to stop

public void shutDown(){

     isShutingDown = true
    }

Upvotes: 0

Erdinç Taşkın
Erdinç Taşkın

Reputation: 1578

There is no stopThread() api on java anymore. You should set a flag as volatile, and your stop thread button just set flag variable value.

public volatile boolean flag;

public void run() {
   while(flag) {
       // Get coordinate or whatever you want      
   }
}

Upvotes: 4

Related Questions