How to get selected radio button from ToggleGroup

后端 未结 5 879
一生所求
一生所求 2020-12-09 16:24

I an working on JavaFX 8 and SceneBuilder. I created some radio buttons in the FXML File and specified a toggleGroup name to a radio button list in

5条回答
  •  长情又很酷
    2020-12-09 16:52

    The solution I got is:

    ToggleGroup toggleGroup = new ToggleGroup();

    Then set toggleGroup to the radio button; let's say r1 and r2

    r1.setToggleGroup(toggleGroup);
    
    r2.setToggleGroup(toggleGroup);
    
    toggleGroup.setUserData(toggleGroup.getSelectedToggle().toString());
    

    To print the data of RadioButton:

    System.out.println("Selected Radio Button is " + toggleGroup.getUserData().toString());
    

提交回复
热议问题