Using Radio-Style Buttons
Radio buttons are similar to Check buttons, except that only one button of a group of buttons may be selected at any given time. You use Radio buttons to enable the user to select one, and only one, of a group of options.
How do I do that?
You create Radio buttons simply by specifying the
SWT.RADIO
style:
final Button b1 = new Button(s, SWT.RADIO); b1.setBounds(100,50,75,20); b1.setText("Check Me"); final Button b2 = new Button(s, SWT.RADIO); b2.setBounds(100,75,100,20); b2.setText("No, Check Me");
Changing the ButtonExample
code to include the
preceding code will display Figure 6-3.
Figure 6-3. Radio buttons
What just happened?
In addition to the Radio button look and feel, you see that it is no
longer possible to check both buttons at the same time. Checking one
will cause the other to automatically become unchecked. For this
reason, radio buttons must belong to a group. A
group is all buttons with the
RADIO
style that
are placed within the same container. In
ButtonExample
the container is a
Shell
, which means that all radio buttons placed
on the shell belong to the same group and will share mutually
exclusive selectability.
You may foresee a problem. What if you need to create two different
groups to enable the user to specify one option from two sets of
options? In this case, the solution is to place the buttons on two
separate containers—Composites
or
Get SWT: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.