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(10, 5, 170, 30);
final Button radioButton1 = new Button(shell, SWT.RADIO);
radioButton1.setText("Java");
radioButton1.setBounds(10, 30, 75, 30);
final Button radioButton2 = new Button(shell, SWT.RADIO);
radioButton2.setText("C/C++");
radioButton2.setBounds(10, 55, 75, 30);
final Button radioButton3 = new Button(shell, SWT.RADIO);
radioButton3.setText(".NET");
radioButton3.setBounds(10, 80, 75, 30);
final Button radioButton4 = new Button(shell, SWT.RADIO);
radioButton4.setText("PHP");
radioButton4.setBounds(10, 105, 75, 30);
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|
Output will be displayed as:
Download Source Code