I am having trouble creating a ButtonGroup containing radio buttons in the Scala Programming Language. The code I am using is as following:
val buttongroup = new ButtonGroup {
buttons += new RadioButton("开发者_StackOverflowOne")
buttons += new RadioButton("Two")
}
and my code for displaying the button group is within a BorderPanel:
layout += new BoxPanel(Orientation.Vertical) {
buttongroup
} -> BorderPanel.Position.West
However, nothing displays... I've consulted the API and I'm not sure what is wrong!!
You should add a list containing the buttons to the panel, not the buttongroup itself, e.g.:
val radios = List(new RadioButton("One"), new RadioButton("two"))
layout += new BoxPanel(Orientation.Vertical) {
contents ++= radios
}
See also this example in the scala swing package itself.
While the button group makes the buttons mututaly exclusive, you still need to add individual buttons to the panel. You can use ButtonGroup.buttons
to obtain the list of the buttons:
layout += new BoxPanel(Orientation.Vertical) {
val buttongroup = new ButtonGroup {
buttons += new RadioButton("One")
buttons += new RadioButton("Two")
}
contents ++= buttongroup.buttons
} -> BorderPanel.Position.West
If you want the first button to be selected when the toolbar is created, you can add:
buttongroup.select(buttongroup.buttons.head)
精彩评论