0

how do i listen on events on a radiobutton in scala? i have the following code, but for some reason the reaction is not executed. this is a Dialog, and i am looking to listen on radiobutton selection, and to change the Dialog window's title accordingly.

val dirFileSelector = {
  List(
    new RadioButton("Directory"){
      name = "dir"

    },
    new RadioButton("File"){
      name = "file"
    }
  )
}

val buttonGroup = new ButtonGroup
dirFileSelector map { button=>
  listenTo(button)
  buttonGroup.buttons.add(button) 
}

contents = new BorderPanel{

  add(new BoxPanel(Orientation.Horizontal) {contents ++= dirFileSelector}, BorderPanel.Position.North)
}

reactions += {
  case SelectionChanged(buttonSelect) => {
    println("buttonSelect selection changed")
    buttonSelect.name match {
      case "dir" => title = "Add Directory"
      case "file" => title = "Add File"
    }
  }

}
Ehud Kaldor
  • 753
  • 1
  • 7
  • 20

1 Answers1

2

As far as I can tell, RadioButtons don't emit the SelectionChanged event. They do however emit ButtonClicked.

This is a simple working example to get the effect you want:

import swing._
import swing.event._

object app extends SimpleSwingApplication {
  val dirFileSelector = List(
    new RadioButton() {
      name = "dir"
      text = "Directory"

    },
    new RadioButton() {
      name = "file"
      text = "File"
    }
  )

  new ButtonGroup(dirFileSelector: _*)

  def top = new MainFrame {
    title = "Test"
    contents = new BoxPanel(Orientation.Horizontal) {
      contents ++= dirFileSelector
    }
    dirFileSelector.foreach(listenTo(_))
    reactions += {
      case ButtonClicked(button) => {
        button.name match {
          case "dir" => title = "Add Directory"
          case "file" => title = "Add File"
        }
      }
    }
  }
}
Daan
  • 1,516
  • 12
  • 20