Scala Swing - botón con acción

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.text) { 
  //...
}

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

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

El compilador dice:

[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.

preguntado el 28 de agosto de 11 a las 01:08

1 Respuestas

Mira el 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.

Respondido 28 ago 11, 05:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.