Quantcast

[scala] Swing (event-thread) actors

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[scala] Swing (event-thread) actors

Victor Rodriguez
Hello,

The subject of Scala actors that perform their body in the event
dispatch thread of Swing (AWT, really) has come up before, but
apparantely with no conclusion.

The following sketches a possible solution.  Will it work?  Or am I
missing something that makes this approach unworkable?

abstract class SwingActor extends Actor {
  override def react( f : PartialFunction[Any, Unit] ) : Nothing =
    super.react( new DispatchThreadPartialFunction( f ) )

  // need to override reactWithin, receive, receiveWithin..

  class DispatchThreadPartialFunction( f : PartialFunction[Any, Unit]
) extends PartialFunction[Any, Unit] {
    import java.awt.EventQueue

    def isDefinedAt( x : Any ) = f.isDefinedAt( x )

    def apply( x : Any ) : Unit =
      EventQueue.invokeLater {
        new Runnable() { def run() = f( x ) }
      }
  }
}


Best Regards,

Victor Rodriguez.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [scala] Swing (event-thread) actors

Philipp Haller-2
Hi Victor,

Victor Rodriguez wrote:
> The subject of Scala actors that perform their body in the event
> dispatch thread of Swing (AWT, really) has come up before, but
> apparantely with no conclusion.

With Scala 2.7.2 this is now very easy to do, thanks to overridable
schedulers:

  abstract class SwingActor extends Actor {
    override val scheduler = new SchedulerAdapter {
      def execute(fun: => Unit) {
        EventQueue.invokeLater {
          new Runnable() { def run() = fun }
        }
      }
    }
  }

Cheers,
Philipp

Loading...