开发者

Scala Swing - button with action

I've created a button with title and icon in my view.

object playButton extends Button("play") {
  icon = new ImageIcon(getClass.getResource("/Play.gif"))
  verticalTextPosition = Alignment.Bottom
  horizontalTextPosition = Alignment.Center
}

Now I want to add it some action in the controller.

view.playButton.action = Action(view.playButton.t开发者_运维技巧ext) { 
  //...
}

The problem is, that this action overrides buttons icon. So... i tried:

view.playButton.action = Action(view.playButton.text) { 
  icon = view.playButton.icon
}

Compiler says:

[info] Compiling main sources...
[error] .../Controller.scala:11: not found: value icon
[error]     icon = view.playButton.icon
[error]     ^
[error] one error found

What am I doing wrong? Action in documentation has this setter for icon field: http://www.scala-lang.org/api/current/scala/swing/Action.html.


Look at the source for scala.swing.Action

In the companion object:

def apply(title: String)(body: =>Unit) = new Action(title) { 
  def apply() { body }
}

In other words, as a convenience to you, they take the block (where you put icon = ...) and make that be the event handler for the Action.

What you actually want to do is subclass:

new Action("Hello") {
    icon = ...

    def apply() = ...
}

This does not appear to be documented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜