SWT: programmatically set switches

When I create a pair of radio buttons ( new Button(parent, SWT.RADIO) ) and programmatically select a selection using radioButton5.setSelection(true) , the previously selected radio button also remains selected. Do I need to iterate over all the other radio buttons of the same group to deselect, or is there a simpler alternative? Thanks in advance.

+7
source share
3 answers

Unfortunately, you need to iterate over all parameters. The first time your user interface appears, the BN_CLICKED event BN_CLICKED . If your Shell or Group or any switch container is not created using the SWT.NO_RADIO_GROUP option, the following method is called:

 void selectRadio () { Control [] children = parent._getChildren (); for (int i=0; i<children.length; i++) { Control child = children [i]; if (this != child) child.setRadioSelection (false); } setSelection (true); } 

Thus, the eclipse itself depends on the iteration of all the switches and their state switching.

Each time you manually select a radio button, the BN_CLICKED event BN_CLICKED and, therefore, automatic switching.

When you use button.setSelection(boolean) then the BN_CLICKED event is not BN_CLICKED . Therefore, automatic switching of the switches does not occur.

Read more in the org.eclipse.swt.widgets.Button class.

+6
source

Radio buttons within the same composite will act as a group. Only one switch will be selected at a time. Here is a working example:

  Composite composite = new Composite(parent, SWT.NONE); Button btnCopy = new Button(composite, SWT.RADIO); btnCopy.setText("Copy Element"); btnCopy.setSelection(false); Button btnMove = new Button(composite, SWT.RADIO); btnMove.setText("Move Element"); 
+1
source

This should happen automatically. How do you create buttons? Are they the same parent? Is a parent using style NO_RADIO_GROUP?

-2
source

All Articles