Select a Radio Button : Button « SWT « Java Tutorial






If you need to select a button programmatically, use the following method:

public void setSelection(boolean selected)

This method is applicable for checkbox buttons, radio buttons, and toggle buttons only. It has no effect on push buttons and arrow buttons.

To check the selection status of a button, use the following method:

public boolean getSelection()

The above method is only applicable for check buttons, radio buttons, and toggle buttons. It always returns false if the button is a push button or an arrow button.

Select a Radio Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class RadioButtonSelection {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());

    for (int i = 0; i < 4; i++) {
      Button button = new Button(shell, SWT.RADIO);
      button.setText("Button " + i);

      button.setSelection(true);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}








17.9.Button
17.9.1.Introducing Button and Button Styles
17.9.2.Push ButtonPush Button
17.9.3.CheckBox ButtonCheckBox Button
17.9.4.Toggle ButtonToggle Button
17.9.5.RadioButtonRadioButton
17.9.6.FlatButtonFlatButton
17.9.7.ArrowButtonArrowButton
17.9.8.Add Radio Buttons to a GroupAdd Radio Buttons to a Group
17.9.9.Select a Radio ButtonSelect a Radio Button
17.9.10.Setting Button selectionSetting Button selection
17.9.11.Add Image to ButtonAdd Image to Button
17.9.12.Add Radio Buttons to a Composite to form a groupAdd Radio Buttons to a Composite to form a group
17.9.13.LEFT, RIGHT, CENTER: These three styles specify the text/image alignment in buttons.
17.9.14.To get and set a button's text label
17.9.15.You can include the mnemonic character in the text label.
17.9.16.To escape the mnemonic character &, you can double it and a single '&' will be displayed.To escape the mnemonic character &, you can double it and a single '&' will be displayed.
17.9.17.Set the alignment of the text/image label of a buttonSet the alignment of the text/image label of a button