Search code examples
javajavafxiterationtogglebutton

Iterating a ToggleGroup and setting each ToggleButton's properties


I can't figure out how to get this to work. I'm trying to go through each of the buttons in a ToggleGroup to set their properties (e.g., visible, disabled), but having no luck. There seems to be no way to set the ToggleGroup as a whole.

I've tried several ways to do it, but have had no luck. ToggleGroup's methods only allow for a getToggles(), so I figured that is how I'm going to have to work at this.

mainToggleGroup.getToggles().stream().forEach((button) -> {
    button.setVisible(false);
});

Closest I've gotten, but the button doesn't have any methods to allow me to change the properties.

Any guidance?


Solution

  • Oh, I found my problem just as I was about to submit this! I needed to cast the getToggles result over from a Toggle to a ToggleButton.

    mainToggleGroup.getToggles().stream().map((toggle) -> (ToggleButton)toggle).forEach((button) -> {
        button.setVisible(false);
    });
    

    This seems to work, but is this the proper way to do this?