如何订阅 Action 属性 change with scala.swing listenTo?
How to subscribe to Action property change with scala.swing listenTo?
我有一个 scala.swing Action with a custom property mydomain.color
. I would like to repaint a button which is bound to this Action whenever the property is changed. It should be possible to subscribe to PropertyChangeEvent, however Action
is not a Publisher,因此不能用于 listenTo
。
用正常的 Java 方式和 write a Property Change Listener 是可能的,但是是否有更短的方式? Java beans 可以用作 scala.swing listenTo
的发布者吗?
一个属性改变监听器可以用来将Java beans事件转化为一个swing事件并发布,并不难:
import scala.swing._
import scala.swing.event._
import java.beans.{PropertyChangeListener, PropertyChangeEvent}
case class PropertyChanged(source: AnyRef, propertyName: String, oldValue: AnyRef, newValue: AnyRef) extends Event
trait ActionPropertyPublisher extends Action with Publisher {
actionPublisher =>
class ListenToPropertyChange extends PropertyChangeListener {
override def propertyChange(evt: PropertyChangeEvent): Unit = {
assert(SwingUtilities.isEventDispatchThread)
assert(evt.getSource==actionPublisher.peer)
publish(new PropertyChanged(actionPublisher,evt.getPropertyName,evt.getOldValue,evt.getNewValue))
}
}
peer.addPropertyChangeListener(new ListenToPropertyChange())
}
我有一个 scala.swing Action with a custom property mydomain.color
. I would like to repaint a button which is bound to this Action whenever the property is changed. It should be possible to subscribe to PropertyChangeEvent, however Action
is not a Publisher,因此不能用于 listenTo
。
用正常的 Java 方式和 write a Property Change Listener 是可能的,但是是否有更短的方式? Java beans 可以用作 scala.swing listenTo
的发布者吗?
一个属性改变监听器可以用来将Java beans事件转化为一个swing事件并发布,并不难:
import scala.swing._
import scala.swing.event._
import java.beans.{PropertyChangeListener, PropertyChangeEvent}
case class PropertyChanged(source: AnyRef, propertyName: String, oldValue: AnyRef, newValue: AnyRef) extends Event
trait ActionPropertyPublisher extends Action with Publisher {
actionPublisher =>
class ListenToPropertyChange extends PropertyChangeListener {
override def propertyChange(evt: PropertyChangeEvent): Unit = {
assert(SwingUtilities.isEventDispatchThread)
assert(evt.getSource==actionPublisher.peer)
publish(new PropertyChanged(actionPublisher,evt.getPropertyName,evt.getOldValue,evt.getNewValue))
}
}
peer.addPropertyChangeListener(new ListenToPropertyChange())
}