• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. SWT Toolbars > Adding Check and Radio ToolItems

4.6. Adding Check and Radio ToolItems

It is often helpful to supply the user with visual feedback as to what toolbar buttons have been previously selected. This is generally true when you need the user to select from a set of options. You learned in Chapter 3 how to provide visual cues in menu systems. Use checked menu items to display checkmarks next to items that have been selected. Use radio menu items to enable the user to select one, and only one, option from a group. Both types of item provide a visual indicator of selected options.

You can provide the same type of functionality on your toolbars if you use the SWT.CHECK or SWT.RADIO style when you create a ToolItem instance.

4.6.1. How do I do that?

As with all widgets, you can change the appearance and functionality of ToolItem objects simply by changing the style. Consider the following code:

final ToolItem sep2 = new ToolItem(bar, SWT.SEPARATOR);
final ToolItem checkItem = new ToolItem(bar, SWT.CHECK);
final ToolItem sep3 = new ToolItem(bar, SWT.SEPARATOR);
final ToolItem radioItem1 = new ToolItem(bar, SWT.RADIO);
  radioItem1.setText("Radio 1");
final ToolItem radioItem2 = new ToolItem(bar, SWT.RADIO);
  radioItem2.setText("Radio 2");
final ToolItem sep4 = new ToolItem(bar, SWT.SEPARATOR);
final ToolItem radioItem3 = new ToolItem(bar, SWT.RADIO);
  radioItem3.setText("Radio 3");
final ToolItem radioItem4 = new ToolItem(bar, SWT.RADIO);
  radioItem4.setText("Radio 4");

Adding this code to the version of ToolbarShellExample created by Example 4-1 results in the window shown in Figure 4-4.

Figure 4-4. Toolbar with check- and radio-style buttons

4.6.2. What just happened?

When this window opens, all the toolbar buttons will be in their unselected state, as shown in Figure 4-4. Clicking a Check button will result in the button remaining depressed, as shown in Figure 4-5.

Figure 4-5. A check button in its checked state

This provides a visual indication to the user that the option represented by the button has been selected. Clicking the button a second time returns it to its unselected state.

The behavior for radio buttons is different from that of check buttons. With radio buttons, only one member of the group can be selected at any given time. Buttons that are immediately adjacent to each other are considered to be part of a group. If another ToolItem is created between two radio buttons—even a separator—they will reside in two different groups.

In Figure 4-4, there are two radio groups. The buttons labeled in the code Radio 1 and Radio 2 are in one group, while those labeled Radio 3 and Radio 4 are in a second group. You may select either Radio One or Radio Two, and either Radio Three or Radio Four, but you may not select Radio One and Radio Two, or Radio Three and Radio Four.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint