Swing 如何使用scala向JavaFXUI组件添加侦听器

Swing 如何使用scala向JavaFXUI组件添加侦听器,swing,scala,javafx-2,javafx,observer-pattern,Swing,Scala,Javafx 2,Javafx,Observer Pattern,我正在使用Scala和Publishertrait来监听数据组件上的更改,并将显示发布/更新到swing UI组件(如) 我最近重写了我的程序以使用Javafx2和Scala,并且我不能使用最近的绑定项目Scalafx,因为我的程序只有一部分切换到javafx(嵌入到swing JFrame中) 基于scala.swingPublishertrait,我可以在scala中使用什么样的最佳语法来实现相同(或类似)的行为 您是否有一些链接或简单的示例来说明此方法?目前我的建议是重用实现发布/反应系统

我正在使用Scala和
Publisher
trait来监听数据组件上的更改,并将显示发布/更新到swing UI组件(如)

我最近重写了我的程序以使用
Javafx2
Scala
,并且我不能使用最近的绑定项目
Scalafx
,因为我的程序只有一部分切换到javafx(嵌入到swing JFrame中)

基于scala.swing
Publisher
trait,我可以在scala中使用什么样的最佳语法来实现相同(或类似)的行为


您是否有一些链接或简单的示例来说明此方法?

目前我的建议是重用实现发布/反应系统的
scala.swing
中的模式。 可以从旧的
scala.swing
存储库中的中获取一个示例

其思想是为需要发布事件的所有javafx类创建可能最通用的包装器,并隐式地将这些类转换为该包装器

包装器将定义从javafx侦听器到发布/反应系统的桥接事件的逻辑

示例代码如下所示,基于前面提到的
scala.swing
code

  package scala.fx.react

  object ScalaFxReactive {
      /*
       * Can't say if Node is the best and only class that should be converted.
       * You should make implicit publishers from any fx class that supports the
       * addEventHandler(someEventType)
       */
      implicit def toPublisher(peer: Node): ScalaFxComponentPublisher = new ScalaFxComponentPublisher(peer)

  class ScalaFxComponentPublisher(peer: Component) {

    /**
     * Contains publishers for various mouse events. They are separated for
     * efficiency reasons.
     */
    object mouse {
       /**
        * Publishes clicks, presses and releases.
        */
        val clicks: Publisher = new LazyPublisher {

          lazy val clicked = new EventHandler[MouseEvent] {
            def handle(e: MouseEvent) {
               /*
                *This is the most critical part: you need
                * to check if it's possible to create the swing 
                * event from an fx event, eventually through an
                * implicit conversion
                */
               publish(new MouseClicked(e))
            }
          }

          def onFirstSubscribe() = peer.setOnMouseClicked(clicked)
          def onLastUnsubscribe() = peer.setOnMouseClicked(null)
          /*
           * probably better:
           * def onLastUnsubscribe() = peer.removeEventHandler(MouseEvent.MOUSE_CLICKED)
           */
        }
        /**
         * Publishes enters, exits, moves, and drags.
         */
        val moves: Publisher = new LazyPublisher {
           lazy val entered = new EventHandler[MouseEvent] {
             def handle(e: MouseEvent) {
               publish(new MouseEntered(e))
             }
           }
           lazy val exited = new EventHandler[MouseEvent] {
             def handle(e: MouseEvent) {
               publish(new MouseExited(e))
             }
           }

           /*
            * Need implementation
            */
           lazy val moved = new EventHandler[MouseEvent] {
             ...
           }
           def onFirstSubscribe() {
             peer.setOnMouseEntered(entered)
             peer.setOnMouseExited(exited)
             ...
           }
           def onLastUnsubscribe() {
             peer.setOnMouseEntered(null)
             peer.setOnMouseExited(null)
             ...
           }
         }
       }
     }
然后,您可以像使用
scala.swing一样支持在组件中进行反应

class MyComponent {
    import scala.fx.react.ScalaFxReactive._

    listenTo(node)

    reactions += {
      ...   
    }

}
代码将被视为一个粗略的草图,不会按原样编译。它指出了一个可能的方向,但对于像您这样需要逐步将遗留
scala.swing
应用程序连接到
javafx
库的人来说,完整的解决方案可能是一个非常有趣的库