我正在研究JavaFX 8和SceneBuilder
。我在FXML文件中创建了一些单选按钮,并为其中的单选按钮列表指定了toggleGroup
名称。所以,现在我想在我的控制器中获取toggleGroup
选中的单选按钮,是否需要将所有单选按钮再次作为控制器中的字段,或者仅toggleGroup
对象将让我选中的单选按钮(仅限单选按钮的文本,而不是按钮对象)。
答案 0 :(得分:21)
@FXML
ToggleGroup right; //I called it right in SceneBuilder.
稍后在某个方法中。
RadioButton selectedRadioButton = (RadioButton) right.getSelectedToggle();
String toogleGroupValue = selectedRadioButton.getText();
答案 1 :(得分:14)
假设您有一个切换组和三个属于该组的单选按钮。
ToggleGroup group = new ToggleGroup();
RadioButton rb1 = new RadioButton("RadioButton1");
rb1.setUserData("RadioButton1");
rb1.setToggleGroup(group);
rb1.setSelected(true);
RadioButton rb2 = new RadioButton("RadioButton2");
rb2.setUserData("RadioButton2");
rb2.setToggleGroup(group);
RadioButton rb3 = new RadioButton("RadioButton3");
rb3.setUserData("RadioButton3");
rb3.setToggleGroup(group);
当您从该切换组中选择一个单选按钮时,将调用以下changed(...)
方法。
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {
if (group.getSelectedToggle() != null) {
System.out.println(group.getSelectedToggle().getUserData().toString());
// Do something here with the userData of newly selected radioButton
}
}
});
答案 2 :(得分:9)
这从未得到正确或彻底的回答,所以我想我会发布我得到的解决方案。
在SceneBuilder
中创建单选按钮时,也可以使用SceneBuilder
将它们分配给一个组。通过Controller访问该组的方法是首先在Controller中创建类型为ToggleGroup
的变量,并将其命名为与您在SceneBuilder
中创建的名称完全相同的名称。然后你可以使用它。这是我如何做到的伪代码示例:
// your imports
public class Controller
{
@FXML ToggleGroup myGroup; //I called it myGroup in SceneBuilder as well.
public void myGroupAction(ActionEvent action)
{
System.out.println("Toggled: " + myGroup.getSelectedToggle().getUserData().toString());
}
public void initialize()
{
//whatever initialize code you have here
}
}
虽然从getUserData
属性返回的文本很长。这是一种获取单选按钮名称的方法:
myGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
{
@Override
public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1)
{
RadioButton chk = (RadioButton)t1.getToggleGroup().getSelectedToggle(); // Cast object to radio button
System.out.println("Selected Radio Button - "+chk.getText());
}
});
希望这可以帮助有人在路上......
答案 3 :(得分:0)
public class MyControler {
@FXML
private ToggleGroup myToggleGroup;
@FXML
public void initialize(){
//the simplest way to print current value (text of the selected radio button)
System.out.format("Selected Radio Button: " + ((RadioButton)myToggleGroup.getSelectedToggle()).getText());
//register on change event
myToggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
{
@Override
public void changed(ObservableValue<? extends Toggle> observable, Toggle oldToggle, Toggle newToggle)
{
//print new selected value after change
System.out.println("Selected Radio Button: " + ((RadioButton)newToggle).getText());
}
});
}
//sample method to set radio button (unselect all if group don't contain value)
private void setRadioButtons(ToggleGroup toggleGroup, String value) {
for (Toggle t : toggleGroup.getToggles())
t.setSelected(((RadioButton) t).getText().equals(value));
}
//sample method to set radio button (select existing value or do nothing)
private void setRadioButton(ToggleGroup toggleGroup, String value) {
for (Toggle t : toggleGroup.getToggles())
if(((RadioButton) t).getText().equals(value))
t.setSelected(true);
}
}
答案 4 :(得分:0)
我得到的解决方案是:
ToggleGroup toggleGroup = new ToggleGroup();
然后将toggleGroup设置为单选按钮;假设r1和r2
r1.setToggleGroup(toggleGroup);
r2.setToggleGroup(toggleGroup);
toggleGroup.setUserData(toggleGroup.getSelectedToggle().toString());
要打印RadioButton的数据,请执行以下操作:
System.out.println("Selected Radio Button is " + toggleGroup.getUserData().toString());