我需要创建一个由两个单选按钮组成的组,并获取所选值的值。
只需使用 ToggleGroup
即可。
RadioButton radioButton1 = ...
RadioButton radioButton2 = ...
// TODO: add RadioButtons to scene
ToggleGroup toggleGroup = new ToggleGroup();
radioButton1.setToggleGroup(toggleGroup);
radioButton2.setToggleGroup(toggleGroup);
// listen to changes in selected toggle
toggleGroup.selectedToggleProperty().addListener((observable, oldVal, newVal) -> System.out.println(newVal + " was selected"));
你也可以使用以下方式从ToggleGroup
中检索所选的单选按钮:
toggleGroup.getSelectedToggle()
如果你想从提交按钮的处理程序或类似的地方执行此操作...