如何在javafx中使用单选按钮组?

时间:2017-03-20 16:44:37

标签: java javafx-8

我需要制作一组2个单选按钮,然后检索所选按钮的值。

1 个答案:

答案 0 :(得分:9)

只需使用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()

如果你想从提交按钮的处理程序或类似的东西......

执行此操作