Note that there are some explanatory texts on larger screens.

plurals
  1. POInteracting with actors in scala swing applications
    primarykey
    data
    text
    <p>I'm writing a small application in scala. The application processes simple log files. Because the processing takes some time, I've decided to let my application core extend Actor. </p> <pre><code>class Application extends Actor { def react() { loop { react { case Process(file) =&gt; // do something interesting with file... } } } } </code></pre> <p>The processing of a log file is triggered by clicking a button in the gui. The gui uses scala swing. </p> <pre><code>object Gui extends SimpleSwingApplication { val application = new Application().start() def top = new MainFrame { val startButton = new Button reactions += { case ButtonClicked(`startButton`) =&gt; application ! Process(file) } } } </code></pre> <p>Now, the application core needs to notify the gui about the current progress. </p> <pre><code> sender ! Progress(value) // whenever progress is made </code></pre> <p>I've solved this by creating a separate actor inside the gui. The actor is executed inside the edt thread. It listens to messages from the application core and updates the gui. </p> <pre><code> object Gui extends SimpleSwingApplication { val actor = new Actor { override val scheduler = new SchedulerAdapter { def execute(fun: =&gt; Unit) { Swing.onEDT(fun) } } start() def act() { loop { react { case ForwardToApplication(message) =&gt; application ! message case Progress(value) =&gt; progressBar.value = value } } } } } </code></pre> <p>Since the application core needs to know about the sender of the message, I also use this actor to forward messages from the gui to the application core, making my actor the new sender.</p> <pre><code> reactions += { case ButtonClicked(`startButton`) =&gt; actor ! ForwardToApplication(Process(file)) } </code></pre> <p>This code works just fine. My question: Is there a simpler way to do this? It whould be nice to simple use the reactions mechanism for my application messages: </p> <pre><code> reactions += { case Progress(value) =&gt; progressBar.value = value } </code></pre> <p>Any ideas how to achieve this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload