[英]Get All JRadioButton from a ButtonGroup
If we consider i have a ButtonGroup
component, which have two JRadioButton
like this : 如果我们认为我有一个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);
How can I get all the JRadioButton
components from the ButtonGroup
ordered by the default order so I can set the first JRadioButton
Selected? 如何从按默认顺序排序的ButtonGroup
获取所有JRadioButton
组件,以便设置第一个选定的JRadioButton
?
Finally i found the solution, i think there are a way to return Enumeration<AbstractButton>
, so use it to return all the JRadioButton
of this ButtonGroup
最后,我找到了解决方案,我认为有一种方法可以返回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.