Create Radio Buttons in SWT

In SWT, the style RADIO defined in the Button class allows to create radio button. We have create an array object of Button class. The class Label displays the specified string.

Create Radio Buttons in SWT

Create Radio Buttons in SWT

     

This section illustrates you how to create radio button.

In SWT, the style RADIO defined in the Button class allows to create radio button. We have create an array object of Button class. The class Label displays the specified string. The method label.setText() sets the string specified. The setBounds() sets the location.

The method radioButton[].setSelection(true) sets the selection state.

Here is the code of RadioButtonExample.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

public class RadioButtonExample {

  public static void main(String[] args) {
  final Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Radio Buttons");
  shell.pack();
 
  final Label label = new Label(shell, SWT.NONE);
  label.setText("Which programming language you like most");
  label.setBounds(10517030);
  final Button radioButton1 = new Button(shell, SWT.RADIO);
  radioButton1.setText("Java");
  radioButton1.setBounds(10307530);
  
  final Button radioButton2 = new Button(shell, SWT.RADIO);
  radioButton2.setText("C/C++");
  radioButton2.setBounds(10557530);
  
  final Button radioButton3 = new Button(shell, SWT.RADIO);
  radioButton3.setText(".NET");
  radioButton3.setBounds(10807530);
  
  final Button radioButton4 = new Button(shell, SWT.RADIO);
  radioButton4.setText("PHP");
  radioButton4.setBounds(101057530);
  
  shell.open();
  shell.pack();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  display.dispose();
  }
}

Output will be displayed as:

Download Source Code