站内搜索: 请输入搜索关键词
当前页面: 图书首页 > SWT: The Standard Widget Toolkit

7.5 Class Combo - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

7.5 Class Combo

7.5.1 Example

graphics/07inf10.gif

7.5.2 Combo Hierarchy

graphics/07inf11.gif

7.5.3 Combo Styles

Style

Description

SWT.READ_ONLY

Make the control noneditable

SWT.DROP_DOWN

The list drops down when needed

SWT.SIMPLE

The list is always visible


7.5.4 Combo Events

Event

Description

SWT.Selection

The user selected an item

SWT.DefaultSelection

Default selection occurred (user pressed <Enter>)

SWT.Modify

Text has changed in the control


A combo control, also called a combo box, is the combination of a single-line text and a single-select list control.[14] The user can type characters into the text control or choose an item from the list, causing it to appear in the text control. The text control can be read-only, in which case the user can select only an item from the list. Normally, combo controls include a small drop-down indicator that is used to hide and show the list. On some platforms when the SWT.SIMPLE style is specified, the list is always visible. Other platforms do not support this hint.

[14] Most platforms actually implement combo controls this way, except for Microsoft Windows. On Windows, when a combo box is not editable, the text is drawn in the combo box control instead of using an embedded single-line text control. This means that SWT cannot provide an API that exposes the underlying controls. Nevertheless, describing the API in terms of these two controls makes it easier to explain.

A combo control that is created with the style SWT.READ_ONLY causes the text portion of the combo box to be noneditable, just like a read-only text control.

When to Use a Combo

Any place where a single-select list could be used, a drop-down read-only combo can be used instead. This will conserve screen real estate. Drop-down read-only combos can also be used in places where you might use a group of radio buttons. In this case, the text field portion of the combo is analogous to the currently selected radio button. The disadvantage of using combos this way occurs when the list needs to be accessed frequently. Also, it is not possible to see at a glance the options that are available because they are hidden in the list.

A drop-down editable combo can be used instead of a single-line text control. To save the user typing, commonly used strings are made available in the drop-down list. Sometimes a program will update the list to include the last string that the user typed.


Combo boxes support the SWT.Selection and SWT.DefaultSelection events. The selection event behaves just like selection for lists, whereas default selection behaves like default selection in a text control.

7.5.5 Combo Is a Combination of API

Combo is more than just a combination of Text and List. The API provided by Combo in SWT is also a combination of the API provided by those two classes. Combo reuses names and concepts from them, so knowing the API of List and Text means that you already know the naming and the major concepts for combos. The downside of this is that it is possible to confuse methods that operate on the text control with methods that operate on the list, and vice versa.

7.5.6 Text Methods

The following methods apply to the text control portion of a combo control and behave in a manner that is similar to methods with the same name in the class Text.

  • clearSelection()

  • copy()

  • cut()

  • getSelection()

  • getText()

  • getTextHeight()

  • getTextLimit()

  • paste()

  • setSelection(Point)

  • setText(String)

  • setTextLimit(int)

7.5.7 List Methods

The following methods apply to the list control portion of a combo. These behave in a manner that is similar to methods of the same name in class List.

  • add(String)

  • add(String, int)

  • deselect(int)

  • deselectAll()

  • getItem(int)

  • getItemCount()

  • getItemHeight()

  • getItems()

  • getSelectionIndex()

  • indexOf(String)

  • indexOf(String, int)

  • remove(int)

  • remove(int, int)

  • remove(String)

  • removeAll()

  • select(int)

  • setItem(int, String)

  • setItems(String[])

Notice that although the select() and deselect() methods apply to the list control portion of the combo, the setSelection() method applies to the text control.

7.5.8 Resizing a Drop-Down Combo

Drop-down combo boxes have the interesting property of having a very specific preferred height. This is the height of the text field plus any extra trimming that the combo might have added. For this reason, some platforms refuse to resize a combo to be smaller than its preferred height.[15] SWT enforces this behavior on every platform in order to be consistent across platforms.

[15] Microsoft Windows has this behavior.

7.5.9 Combo Events

SWT.Selection (SelectionEvent)

The SWT.Selection event (typed event SelectionEvent) is sent whenever the user selects a list item with the mouse or the keyboard. The selection event for Combo contains meaningful values in only the display, widget, and type fields.

SWT.DefaultSelection (SelectionEvent)

The SWT.DefaultSelection event (typed event SelectionEvent) is sent whenever the user performs the platform-specific operation that indicates default selection. On most platforms, default selection occurs when the user presses the <Enter> key. The default selection event for Combo contains meaningful values in only the display, widget, and type fields.

Using SWT.DefaultSelection to Keep a "Recently Typed" List

The following example code (results shown in Figure 7.8) uses the SWT.DefaultSelection event to add the strings that the user types in the text control to the list portion of the combo control. Combo controls allow duplicate items. This means that we need to check that the string is not already present before adding it. To stop the list from growing without bound, a limit on the number of strings is enforced. When the limit is reached, the oldest string is deleted from the list.






static final int LIMIT = 10;

public static void main(String[] args) {

    Display display = new Display();

    Shell shell = new Shell(display);

    final Combo combo = new Combo(shell, SWT.DROP_DOWN);

    combo.pack();

    Point size = combo.getSize();

    combo.setSize(200, size.y);

    combo.addListener(SWT.DefaultSelection,new Listener() {

        public void handleEvent(Event event) {

            String text = combo.getText();

            if (combo.indexOf(text) == -1) {

                if (combo.getItemCount() >= LIMIT) {

                      combo.remove(0);

                }

                combo.add(text);

            }

        }

    });

    shell.pack();

    shell.open();

    while (!shell.isDisposed()) {

        if (!display.readAndDispatch()) display.sleep();

    }

    display.dispose();

}


Figure 7.8.

graphics/07fig08.gif


When to Use SWT.Selection and SWT.DefaultSelection

When a combo is read-only, the text field can be changed by the user only through the list, causing the SWT.Selection event. This means that for a read-only combo, it is appropriate to invoke an action from SWT.Selection. It is also reasonable (but not necessary) to invoke the same action from the SWT.DefaultSelection event, rather than forcing the user to reselect the item.

When a combo is editable, the case for using the SWT.Selection event to invoke an action is less clear. For example, when the string in the text control matches an item in the list, there is ambiguity. The user may have entered the string or selected the item. In the case of a Web browser that uses a combo to get a URL, the SWT.Selection event should navigate to the Web page. When a combo is prompting for a file name, perhaps to run a program, the SWT.Selection event should probably not invoke the program. In both cases, it is appropriate to use SWT.DefaultSelection to invoke the action.

SWT.Modify (ModifyEvent)

Table 7.3 shows the modify event for the combo control.

Table 7.3. Modify Event

Untyped Event

Description

SWT.Modify

Text has changed in the control

Typed Event

Listener

Methods

ModifyEvent

ModifyListener

modifyText(ModifyEvent)


The SWT.Modify event (typed event ModifyEvent) is sent after characters have been inserted or deleted from a combo control. The event is sent both when the user types and when the program changes the control. Modify events contain meaningful values in only the display, widget, and type fields.

The SWT.Modify event for combo controls is used in the same manner as SWT.Modify for text controls. The SWT.Modify example in the Text Events section can easily be changed to use an editable combo instead of a text control. The implementation is left as an exercise for the reader.

    Previous Section  < Day Day Up >  Next Section