|
|
< Day Day Up > |
|
7.5 Class Combo7.5.1 Example
7.5.2 Combo Hierarchy
7.5.3 Combo Styles
7.5.4 Combo Events
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.
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.
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 APICombo 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 MethodsThe 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.
7.5.7 List MethodsThe 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.
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 ComboDrop-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.
7.5.9 Combo EventsSWT.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" ListThe 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.
When to Use SWT.Selection and SWT.DefaultSelectionWhen 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.
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. | |||||||||||||||||||||||||||||||||
|
|
< Day Day Up > |
|