Note that there are some explanatory texts on larger screens.

plurals
  1. POStopping a thread by a swing button
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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.</p> <p>The code as it is : </p> <p>The class running the thread</p> <pre><code>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(); } } </code></pre> <p>The mouse locate class:</p> <pre><code>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; } </code></pre> <p>}</p> <p>Thank you for any help I really appreciate it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload