views:

61

answers:

2

I have a JWindow and a JFrame both I have made runnable and both implement a mouse listener. I have alot of testing to do for a project of mine and to simplify it I wish to be able to automate most of it, so I have starting my own mouse recorder and replayer (uses Java Robot Class).

Kinda like a simplified AutoHotKey or AutoIt thing... but it'll run on my Ubuntu machine aswell as my Windows one!!!

The JWindow I have made is translucent and covers the entire screen, when you click on it it disappears and replays the click to the object behind and then reappears. This is the recording process. When the User right clicks the is set to not visible and the recorded actions are replayed.

During replay I want the option to be able to exit the entire application and so I though the best way to do this would be to make the JFrame and the JWindow Runnable.

The JFrame is simply their to offer a close option from the application.

So, in my Main class I have

public static void Main(String[] args){
    recorder = new Recorder();
    gui = new GUI();
    Thread tr = new Thread(recorder);
    Thread tg = new Thread(gui);
    tr.setName("Recorder");
    tg.setName("GUI");
    tr.start();
    tg.start();
}

My understanding is Recorder and GUI are runnable objects and they are made into threads via the new Thread command. When I use .start() I am beginning the execution of the thread and from here on the system decides which thread is running at any particular time.

Onto the Recorder and GUI classes.

public class Recorder
        implements Runnable, MouseListener {

//Constructor and other code

    public void mouseClicked(MouseEvent e) {

        if (e.getButton() == MouseEvent.BUTTON1) {
             //Record events
        }else{
             //Replay events
        }
        System.exit(0);
    }

    public void run() {
        System.out.println("Recorder");
    }
}

public class GUI 
    implements Runnable, MouseListener {

//Constructor, simply constructs JFrame and sets mouselistener to it

   public void mouseClicked(MouseEvent e) {
       System.exit(0);
   }

   public void run() {
        System.out.println("GUI");
   }

}

My application prints out Recorder and then GUI Allows me to record my events then right click on the JWindow to replay them...

but then when I click on the JFrame's close button or even in the frame because of the mouse listener it won't exit until all of the actions have been fully replayed?

One thing I did wonder is what ever I put in run is what is what keeps the thread running? So when System.out.println(""); is executed the thread dies? So I tried a while loop around them and my application successfully prints

GUI GUI GUI RECORDER RECORDER GUI RECORDER etc etc

So I can see that they threads are running concurrently... it's just that all of the other actions outside of the run don't seem to get executed... How can I include my mouse listener, etc within the threads execution?

+1  A: 

You are confusing Threads with Objects. When you have an Object that is a Runnable that just gives a starting point for a Thread. However this doesn't mean that when another thread (in this case the Event thread that handles the MouseListener) calls a method in your Runnable that it executed by the thread that is executing the Runnable. When a method is called, it never switches to another thread. If you want this you need a mechanism. For instance a queue that the MouseListener can post tasks to, and in your Runnable.run() you then keep looking for a new task.

Thirler
ok so as soon as I enter a while loop within the mouse event methods, or enumeration within a Runnable Object that is executed and the thread does not continue to executed both threads until the task has completed within the mouseevent hander.e.g. If I right click on the recorder Runnable Object my enumeration code is executed to playback every action, the recorder thread executes the enumeration. the GUI is unresponsive because the recorder thread is running the whole enu and the GUI thread cannot run until the enumeration has stopped or a mechanism successfully handles the interruption.
Emdiesse
+1  A: 

When swing initializes it starts it's own Event Dispatch thread. All your listener methods executes within this thread. It doesn't matter whether or not your listener object implements runnable.

See this tutorial to grasp the basics of multi-threading in context of Swing. http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

The actual answer to your question is in this part of the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html

But I suggest that you go through the whole tutorial.

Tahir Akhtar
Thank you, i'll read the whole tutorial. I have looked through that previously to try to find what it was that I wanted, because I thought I had grasped the concept of threads... but now it is apparent that I have not :)
Emdiesse