Skip to content

Instantly share code, notes, and snippets.

@nightscape
Forked from fnumatic/Gui.scala
Created November 29, 2012 01:44
Show Gist options
  • Save nightscape/4166207 to your computer and use it in GitHub Desktop.
Save nightscape/4166207 to your computer and use it in GitHub Desktop.
Simple Swing Application with scala.react * github.com/ingoem/scala-react
This is based on the scala.react library.
It should print "click" on the console every time you click the button.
import MySwingDomain._
object Gui extends ReactiveSwingApp {
val button = new MyButton("test")
def top = new MainFrame {
contents = new FlowPanel {
contents += button
}
}
schedule { ConsoleObserver().obs }
}
case class ConsoleObserver() extends Observing {
def obs {
observe(Gui.button.actionPerformed)(_ => println("click"))
}
}
import MySwingDomain._
class MyButton(text: String) extends Button(text) with HasAction
trait HasAction extends Observing {
this: AbstractButton =>
def getAction() = peer.getAction
def addActionListener(a: ActionListener) = peer.addActionListener(a)
def removeActionListener(a: ActionListener) = peer.removeActionListener(a)
val actionPerformed: Events[Action] =
new EventSource[Action](MySwingDomain.owner) {
source =>
addActionListener(new ActionListener {
def actionPerformed(e: ActionEvent) = { source << getAction }
})
}
}
object MySwingDomain extends SwingDomain {
self:Domain =>
val scheduler = new SwingScheduler()
val engine = new Engine
}
abstract class SwingDomain extends Domain {
trait ReactiveSwingApp extends SimpleSwingApplication {
override def main(args: Array[String]) {
schedule { startup(args) }
start()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment