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 8369479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:39:18+00:00 2026-06-09T13:39:18+00:00

I made a simple program which just tracks the co ordinates of the mouse

  • 0

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.

  • 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-09T13:39:20+00:00Added an answer on June 9, 2026 at 1:39 pm

    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      
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started on AWT and made a simple program in it, it
I just made a very simple test app using documentation from MSDN. All I
I have made my own file type (.ddd) and I made a simple program
So I made this (very simple) program with a swing GUI with NetBeans, and
Hello I made a simple racket program on windows xp sp3 I wonder how
For an assignment I've made a simple C++ program that uses a superclass (Student)
I recently made a very simple practice program in Python, that takes user input
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,
I'm writing a simple program in Java which includes a KeyListener with the following
I have made a simple, test program to experiment using classes. It creates a

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.