开发者

Invoke a code after all mouse event listeners are executed

I have created a panel on which a set of objects is drawn. Each of the object is added as a mouse event listener to the panel. As I know, af开发者_开发百科ter an event occurs the listeners are notified and the code might be (or is?) executed in several threads. Is it possible to attach a custom code that will be executed after all listeners finish executing their code?


Is executed in the same thread ( the event dispatcher thread ) .

To do what you want you just have to add an extra listener and have that listener invoke "executeLater" method of SwingUtilities class.

What is does, is wait for the EDT thread finish the notifications and then invoke the your code.

To test it, add this listener and see what it does:

 class MouseListener extends MouseAdapter {
     public void mouseClicked(MouseEvent e)  {
           // System.out.println("If uncommented this would be invoked with the rest of the listners");
           SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    System.out.println("Invoked after all the listeners were notified");
                }
            }
     }
 }

Granted, it is a mouse listener what you've got. The concept is the same for all the other listeners.


As for Noel comment:

A concern that this technique might trip on is if a particular listener implementation returns a notification early, and executes work "out-of-band" on a separate thread. In that case, the end of the notification call does not actually mark the end of the listener's execution. If this is an actual concern (i.e, you're aware of and concerned with other user-defined, elaborate listeners that do this sort of thing, you might want to refine and revisit the problem.

Using the SwingUtilities.invokeLater() you are sure the code is executed after all the listener were notified. But one of these listeners may perform it's job in a separated thread.

If you need to execute your code after all the listeners not only were notified but they finished their job you could do something like this:

pseudocode:

Listener implements MouseListener 
    +mouseClicked( event: MouseEvent ) 
        SwingUtilities.invokeLater( // returns immediately 
              someTask() 
        )

    -someTask()
        // perform some long task.

If you have your listeners as

 addListener( new Listener() )
 addListener( new Listener() )
 addListener( new Listener() )
 addListener( new ExecuteAtTheEnd() ) 

If all ( or some ) of your listeners perform their job in a different thread. Your ExecuteAtTheEnd may be executing it's code at the end of the notification, but not at the end of the execution of the listeners code.

So, what you do?

You have to synchronize the usage of some lock flag and execute your code until that flag is not used.

It may be a counter that is incremented when the listener is executing and decremented when is not:

 Listener implements MouseListener 
    +mouseClicked( event: MouseEvent )
        lockFlagCount++ 
        SwingUtilities.invokeLater( 
              someTask() 
        )

    -someTask()
        // perform some long task.
        lockFlag--

And then you just proceed until this flag is false:

  ExecuteAtTheEndListener implements MouseListener 

       + mouseClicked( event: MouseEvent ) 
        SwingUtilities.invokeLater( 
              executeAtTheEnd() 
        )
       - executeAtTheEnd() 
            while( logFlagCount > 0 ) 
                wait()

             if( logFlagCount == 0 ) 
                 // continue with the task.                  

Of course it is much more complicated as I put it in the pseudo-code, but it should work, if you are in this kind of circumstance.


I believe that what you will want is to use the SwingUtilities.invokeLater() method which will invoke a Runnable instance on the Event Dispatch Thread after all other GUI events have been processed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜