2

I have a JFrame that is my main application, the application moves the mouse via the Robot class. I would like to run my class that extends Thread and have it be able to start, pause, and stop a predefined list of mouse moving events, and also be able to write in the main JFrames text area to let the user know what is happening.

Currently I start the thread in an infinite loop of mouse movement, with updates to the main text area throughout the loop. It worked well but after started rewriting my application in Netbeans the designer I used to design the JFrames is using a, I think, swing text area which is not updating its text while the thread MouseMover is running.

I have defined the class as such

public class MouseMover extends Thread 
{

    MainView theApp;

    public void run()
    {
        try 
        {
            rob = new Robot();
        } 
        catch (AWTException e1) {mainView.newStatusLine("The Robot could not be created");return;}


        while(true) {}
    }

    public void setApp(MainView view)
    {
        theApp = view;

    public void doIt()
    {     
        while(true){
        // move mouse around screen
        rob.mouseMove(0, 100);
        theApp.statusTextArea.setText("moved the mouse");
        }
    }
}

And I define the thread as such

public class MainView extends javax.swing.JFrame 
{
       static MouseMover mover;

       public MainView() 
       {
          mover = new MouseMover();
       }

       public void on_goBtn()
       {
           mover.doIt();
       }
 }

I have been told when asking similar questions I am starting the thread wrong, I am trying to fix it now and add some more functionality. So how do I properly start the MouseMover thread so my JFrame stays responsive while the thread can also write to its text area? And the kicker, how might I be able to "pause" the doIt() method of MouseMover?

assylias
  • 321,522
  • 82
  • 660
  • 783

3 Answers3

2

Instead of mover.doIt(); you need to call mover.start() method to start executing run method in a new thread. Refer here for tutorial on starting threads

And your run method should be

public void run() { // instead of doIt     
    try {
        rob = new Robot();
    } 
    catch (AWTException e1) {mainView.newStatusLine("The Robot could not be created");return;}
    while(true){
    // move mouse around screen
    rob.mouseMove(0, 100);
    theApp.statusTextArea.setText("moved the mouse");
    }
}

Also please read java code conventions to avoid this kind of method names: on_goBtn

Tala
  • 8,888
  • 5
  • 34
  • 38
2

It worked well but after started rewriting my application in Netbeans the designer I used to design the JFrames is using a, I think, swing text area which is not updating its text while the thread MouseMover is running.

  • you have an issue with Concurency in Swing, Swing is designated as single threaded,

  • EDT is special thread, all updates to already visible Swing GUI must be done on EDT, basically Swing GUI doesn't care about you updating JTextArea out off EDT, nothing will be repainted,

  • there are important changes in Java7, most of Thread_Safe methods (up to Java7) aren't thread safe

  • you would need (statusTextArea.setText("moved the mouse"); and AWT.Robot) and I assumed that there is endless loop, then SwingWorker isn't proper of ways

    1. (Java6) invoked from Runnable@Thread output could be wrapped into invokeLater (must be for Java7)

    2. (your code) output must be wrapped into invokeLater from plain vanilla Thread (Java6/7)

    3. see how awt.Robot works in question Do Robot methods need to be run on the event queue? (by @Hovercraft Full Of Eels)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You seems to be missing the thread.start as the first thing in your code, which actually spawns a thread and starts the parallel executing by calling the overriden run method.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136