ButtonGroup + JRadioButton (Java 5): Como deixar todos os radios desmarcados
1 resposta
Mantu
Olá pessoal!
Preciso criar um grupo de radio buttons que tenha a seguinte peculiaridade: Ao clicar em um radio que já esteja selecionado, este deve ser desmarcado, fazendo com que nenhum radio do grupo esteja selecionado.
Inocentemente, tentei dar um setSelected passando false (tanto no radio, quanto em seu modelo, quanto pelo button group) mas o radio não era desmarcado.
Na API do ButtonGroup do Java 6, beleza: Tem um método lá, se não me engano, que faz isso de uma vez (clearSelection ou algo assim…). Porém, meu projeto é em Java 5, e o Button Group dessa versão da linguagem não tem esse método.
Arranjei uma solução mas fiquei me sentindo meio burro… Tive que esvaziar o button group, desmarcar o radio selecionado, e então devolver os radios previamente retirados ao ButtonGroup… Funcionou, mas isso está me cheirando a POG…
Existe uma solução menos zoada que esta?
importjava.io.Serializable;importjava.util.Enumeration;importjavax.swing.AbstractButton;importjavax.swing.ButtonModel;publicclassMyButtonGroupextendsjavax.swing.ButtonGroupimplementsSerializable{/** * The current selection. */ButtonModelselection=null;/** * Creates a new <code>ButtonGroup</code>. */publicMyButtonGroup(){}/** * Adds the button to the group. * @param b the button to be added */@Overridepublicvoidadd(AbstractButtonb){if(b==null){return;}buttons.addElement(b);if(b.isSelected()){if(selection==null){selection=b.getModel();}else{b.setSelected(false);}}b.getModel().setGroup(this);}/** * Removes the button from the group. * @param b the button to be removed */@Overridepublicvoidremove(AbstractButtonb){if(b==null){return;}buttons.removeElement(b);if(b.getModel()==selection){selection=null;}b.getModel().setGroup(null);}/** * Returns all the buttons that are participating in * this group. * @return an <code>Enumeration</code> of the buttons in this group */@OverridepublicEnumeration<AbstractButton>getElements(){returnbuttons.elements();}/** * Returns the model of the selected button. * @return the selected button model */@OverridepublicButtonModelgetSelection(){returnselection;}/** * Sets the selected value for the <code>ButtonModel</code>. * Only one button in the group may be selected at a time. * @param m the <code>ButtonModel</code> * @param b <code>true</code> if this button is to be * selected, otherwise <code>false</code> */@OverridepublicvoidsetSelected(ButtonModelm,booleanb){if(b&&m!=null&&m!=selection){ButtonModeloldSelection=selection;selection=m;if(oldSelection!=null){oldSelection.setSelected(false);}m.setSelected(true);}}/** * Returns whether a <code>ButtonModel</code> is selected. * @return <code>true</code> if the button is selected, * otherwise returns <code>false</code> */@OverridepublicbooleanisSelected(ButtonModelm){return(m==selection);}/** * Returns the number of buttons in the group. * @return the button count * @since 1.3 */@OverridepublicintgetButtonCount(){if(buttons==null){return0;}else{returnbuttons.size();}}@OverridepublicvoidclearSelection(){if(getSelection()!=null){ButtonModeloldSelection=getSelection();selection=null;oldSelection.setSelected(false);}}}