setSelected a specific jradiobutton in a buttongroup based on action command

↘锁芯ラ 提交于 2019-12-01 21:14:14
Spycomb
yourButtonGroup.clearSelection();
yourRadioButton.setSelected(true);

The ButtonGroup#getElements method gives you an enumeration of AbstractButton instances. The AbstractButton#getActionCommand allows you to retrieve the action command and the AbstractButton#getSelected allows you to alter the selection.

So there is a way, but you will have to loop over the enumeration yourself.

Carlos

I created a small method that allow me to set any radio group button. Very convenient if you don't want to use if for any radio button.

public void setButtonGroup(String rdValue, Enumeration elements ){
    while (elements.hasMoreElements()){
        AbstractButton button = (AbstractButton)elements.nextElement();
        if(button.getActionCommand().equals(rdValue)){
            button.setSelected(true);
        }
    }
}

then

setButtonGroup(yourValue, yourButtonGroup.getElements());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!