![]() ![]() ![]() |
Appendix D
Using JCField's Autocomplete Feature
Using Autocomplete in a JCComboField
Autocomplete Methods
Autocomplete Modes
Code Examples
Setting and Updating the List of Autocomplete Strings
Porting Guidelines
D.1 Using Autocomplete in a JCComboField
The autocomplete mechanism in JCField's
JCComboField
may be used to simplify selecting items in a combo box. In addition to providing a facility for narrowing the range of possible matches as each character is typed, and thus anticipating what choice the end user really wants, the prefix mechanism can be used to simplify typing Web addresses, directory paths, or other choices that begin with a common String.Here is what you need to do to use the autocomplete facility. A code snippet is included in each step:
- Create or reference a combo field.
JCComboField combo = new JCComboField());
- Create a String validator, or a validator derived from a String validator. These are the only validators that may be used with the autocompletion mechanism:
JCStringValidator
,JCDateValidator
,JCDateTimeValidator
,JCTimeValidator
,JCIPAddressValidator
:JCStringValidator sv = new JCStringValidator();
- Create or update the list Strings that will populate the combo field's drop down.
String[] string_list = {string1, string2, ...};
- Create a list model with the items.
JCListModel autoCompleteListModel = new JCListModel(string_list);
- Set this list model on the validator.
sv.setPickList(autoCompleteListModel);
- Set the validator on the combo box.
combo.setValidator(sv);
- Once a validator containing the list model has been set on a chosen combo field, the method call for invoking auto completion is:
JCComboField combo;
boolean autoComplete = true;
combo.setAutoComplete(autoComplete, autoAppend, autoSuggest,
autoRefinement, prefix_list);The first four parameters are Booleans, while the last is an array of
String
s. The only way to set the modes and a prefix list is through a call tosetAutoComplete()
. The parameters forsetAutoComplete()
are:
autoComplete
- autocomplete is active. The combo box tries to autocomplete as the user types. If this parameter isfalse
, auto completion is disabled.autoAppend
- a candidate String appears in the text field itself. The drop down list does not appear unlessautoSuggest
is alsotrue
. What the end user has already typed appears as normal text and the rest of the autocompleted String appears in reverse video.autoSuggest
- a drop down list appears as soon as the end user starts typing. All Strings in the list model appear in the list. The selected item is updated as the user types. The list updates itself ifautoRefinement
in on. IfautoAppend
isfalse
there is no candidate completed String in the text field, only the characters the end user has typed so far.autoRefinement
- to use refine, suggest mode must be on. It operates on the drop down list part of the combo box. IfautoRefinement
istrue
, the list updates itself as the end user continues to type, eliminating choices that are no longer relevant. Also, it adds what is currently in the text box to the list. IfautoRefinement
isfalse
, the drop down list retains all items and the first possible match is highlighted. Autorefinement always reacts to adding or deleting a character anywhere in the String as the autocompletion mechanism matches possibilities with what has been typed.prefix_list
- eliminates the need for the end user to type a common first part of the input, such as http://www. in a URL, or C:/JClass/com/klg/class in a directory path. Set a prefix list by callingsetAutoComplete()
with the array of prefix list Strings as the last parameter. If the typed-in String matches the letters immediately following one of the Strings in the prefix list, the item or items matched will appear in the combo box, along with items that begin with the typed-in String. If you are using a prefix list, set the Boolean propertiesautoSuggest
andautoRefinement
totrue
to avoid behavior that might be non-intuitive to the end user. With these two properties set, matching begins as soon as the end user begins typing. The drop down list shows the first matched list item highlighted along with any other possible match. The drop down list updates itself as the end user continues to type, showing only those items that are possible completions to what has already been typed.Example: The prefix list contains the Strings {"water", "snow"}, and the combo box list contains items "police," "polish," "waterpic," "waterpolo." The combo box has
autoSuggest
andautoRefinement
set totrue
. The end user begins by typing a "p" in the text box. All the items mentioned above appear in the drop down list. The last item is the letter "p" that has just been typed in. The end user types an "o," so the text box contains "po." The drop down list updates itself to contain just the matched items "police," "polish," "waterpolo," and "po." By the time the end user has typed "polo," only two list items remain, "waterpolo," and "polo."
D.1.1 Cursor Behavior
Placing the cursor within the String sets the insertion point for the next typed character. The appearance of the cursor and its behavior depend on the mode autocomplete is in. The following table lists the state of
autoSuggest
(S),autoRefine
(R), andautoAppend
(A). The behaviors of the cursor, the text box, and the drop down list are described for each state. Assume in each case thatautoComplete
istrue
.Backspacing
To summarize, if
autoAppend
is false, backspacing deletes a character as usual. There is no autocompletion, so the cursor is at the end of the String. IfautoAppend
istrue
, there are effectively two cursors when a match is first found, one for inserting text and one for deleting text. Characters are inserted just before the highlighted text. Backspaces cause the highlighted text to be erased, after which previous typing is erased.
D.2 Autocomplete Methods
The methods listed here are of use with the autocomplete function:
D.3 Autocomplete Modes
The parameters in
setAutoComplete()
control the autocomplete modes:The autocomplete mechanism for a
combo.setAutoComplete(true, append, suggest, refine, prefix_list);JCComboField
is turned on and off by a call tosetAutoComplete()
. The method's first parameter is the flag that controls whether autocomplete is enabled or not, and the other parameters set the autocomplete modes and the prefix list. If you do not want a prefix list, set the prefix list parameter to null. Thus, a typical call tosetAutoComplete()
looks like this:
D.4 Code Examples
The following example shows a method that returns a
JCComboField
. Its parameters arestring_list
, the list of items for the combo box,prefix_list
, a list of ignorable prefixes, and three Booleans for the autocomplete modes,suggest
,refine
, andappend
.
D.4.1 Explaining the Code
This section further explains the code in the previous section. The line number keys specify which line is being described.
D.5 Setting and Updating the List of Autocomplete Strings
The autocomplete candidates are the Strings that populate the combo box's drop down list. Among the ways of setting the list of Strings on the swing data model, these three deserve notice.
- from predefined Strings set in the application itself
- as the result of a database query
- read from a file containing the appropriate items
Once initialized, you may wish to update the list:
- as the end user types new information
- as information changes as the result of a database query
- because the context of the application has changed
Updating from user input
One way of updating the data model is by adding a
JCComboField combo;JCValueListener
to the combo field. When the end user commits a choice by typing the Enter key, the listener'svalueChanging()
andvalueChanged()
methods are invoked. In thevalueChanged()
method, get the result of the user's input with
...
String newValue = (String) combo.getValue();You will have to check that the entry is different from ones already in the list. If it is, the new entry may be added to the data model at the position you deem appropriate, thereby updating the list. Please see Event Programming, in Chapter 4 for an example of modifying the combo box's pick list from user input as well as the example that follows in this section.
Updating from a database
You can connect a data-aware
JCComboField
component, such asDSdbComboField
, to a data source and populate the data model form an SQL query. Please see the Data Binding, in Chapter 3, for information on data binding.When the database is updated, the data source should fire an event to inform the combo box so that the change may be reflected in its item list.
Populating the list from a file
The tokens representing the list elements you want may be delimited in various ways in your source file. When reading the file, you will form the array of
String
s that you pass to aJCListModel
, and you may want to useJCStringTokenizer
in JClass Elements to simplify the task.JClass Elements is available as part of the JClass DesktopViews product bundle. Visit http://www.quest.com for more information and downloads.
Adding an Item from End User Input
If you wish to allow end users to add to the list items, you can have the combo field respond to a value change event. Here is a suggestion.
In the class that is registered as a listener for
public void valueChanging(JCValueEvent e) {// May be empty }JCValue
events, implement the two required methods,valueChanging()
andvalueChanged()
.
public void valueChanged(JCValueEvent e) {
String newValue = ((String) combo.getValue()).trim();
boolean found = false;
int position = 0;
if(newValue != null && newValue.length() > 0){
for (int i = 0; i < dm.getSize(); i++){
if (newValue.compareTo((String)dm.getElementAt(i)) == 0) {
found = true;
} else {
if (newValue.compareToIgnoreCase(
(String)dm.getElementAt(i)) > 0) {
//place the new item in its sorted place
position = i + 1; }
}
}
}
if (!found && newValue != null && newValue.length() > 0) {
dm.add(position, (String) newValue);
combo.setPickList(dm);
combo.setSelectedIndex(position);
}
}The event handler looks for an existing list item that is essentially the same as the one the end user typed. If one is found, no addition is made. If the user's input is different from each item in the existing list, the new item is added to the data model and to the combo field's pick list.
Note: If
autoRefinement
istrue
, theValueChanged
event is not passed on to your class simply by pressing the Enter key. If you wish to allow end users to update the list of combo box items usingactionPerformed()
whileautoRefinement
is in effect, they will have to click on the newly-typed item. Since it does not match any previous item, it will be the only one remaining in the drop down list. This generates aValueChanged
event that is passed to yourValueListener
.
D.6 Porting Guidelines
There should be no porting issues for your applications that employ a JClass
JCComboField
, version 4.5.1 or earlier, when you update to a current release. Your code should continue to function, and you may add autocompletion if you wish.
![]() ![]() ![]() |