Adding RadioMenuItem to ToggleGroup in FXML
Asked Answered
S

2

18

Somewhere inside my FXML, I have this:

<fx:define>
  <ToggleGroup fx:id="toggleGroup1"/>  
</fx:define>
<Menu fx:id="toggleMyView" text="%MyView">
  <items>
    <RadioMenuItem text="%hide"
                   selected="true"
                   onAction="#handleLoadComponent"
                   toggleGroup="toggleGroup1"/>`

Somehow I get the error:

Unable to coerce toggleGroup1 to class javafx.scene.control.ToggleGroup

But why?

What I'm trying to do is to create a Menu containing several of RadioMenuItem which are all in the same ToggleGroup. How can I add them to the toggle group in my FXML file?

Shingly answered 1/12, 2015 at 0:20 Comment(0)
S
20

You have to write

toggleGroup="$toggleGroup1"
Shingly answered 1/12, 2015 at 1:20 Comment(1)
It also appears to be important that your <fx:define> block that defines your ToggleGroup be above any references to said ToggleGroup.Virtually
C
4

If you are using SceneBuilder then set the field ToggleGroup in properties to the name of the group.

Chalfant answered 13/6, 2018 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.