繁体   English   中英

从ButtonGroup获取所有JRadioButton

[英]Get All JRadioButton from a ButtonGroup

如果我们认为我有一个ButtonGroup组件,它有两个JRadioButton像这样:

JRadioButton bButton = new JRadioButton("Boy");
JRadioButton gButton = new JRadioButton("Girl");
ButtonGroup group = new ButtonGroup();

bButton.setSelected(true);

group.add(bButton);
group.add(gButton);

如何从按默认顺序排序的ButtonGroup获取所有JRadioButton组件,以便设置第一个选定的JRadioButton

最后,我找到了解决方案,我认为有一种方法可以返回Enumeration<AbstractButton> ,因此可以使用它返回此ButtonGroup所有JRadioButton

//Convert Enumeration to a List
List<AbstractButton> listRadioButton = Collections.list(group.getElements());

//show the list of JRadioButton
for (AbstractButton button : listRadioButton) {
    System.out.println("Next element : " + ((JRadioButton) button).getText());
    System.out.println("Is selectd = " + button.isSelected());
}

//Set the first JRadioButton selected
if(listRadioButton.size() > 0){
    listRadioButton.get(0).setSelected(true);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM