views:

97

answers:

1

hello, how can I handle a situation, where a filed listens to a buttons that is not declared yet?

  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 can't declare seznam first either, because it reference the field detail. So how can I write this?

+2  A: 

listenTo is a public method. The easiest thing to do, therefore, is to create them as you've shown above, but add detail.listenTo(button) after you've created 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)
}
Rex Kerr