Scala swing event :: chicken or egg

How can I handle a situation where a given function listens for buttons that have not yet been declared?

  val detail = new BoxPanel(Orientation.Vertical){
    listenTo(button)
  }
  val seznam = new BoxPanel(Orientation.Vertical){
    val button = new Button("But"){
      reactions += {
        case ButtonClicked(_) =>
          detail.contents.clear
          detail.contents += new Label("Anystring")
    }
  }

I cannot declare it first seznambecause it refers to a field detail. So how can I write this?

+5
source share
1 answer

listenTo- publicly available method. Therefore, the easiest way to create them is as shown above, but add detail.listenTo(button)after you create the button:

val detail = new BoxPanel(Orientation.Vertical){ }
val seznam:BoxPanel = new BoxPanel(Orientation.Vertical){
  val button = new Button("But"){
    reactions += {
      case ButtonClicked(_) =>
        detail.contents.clear
        detail.contents += new Label("Anystring")
    }
  }
  detail.listenTo(button)
}
+3
source

All Articles