View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Setting a Specific Index in a List Box when it loads by Sam Asghari

When dealing with GUIs, it is very natural to have lists where users can select a single item. For our project, we thought that it would be nice to have a particular item in the list pre-selected for the user when they open up the GUI. To do this requires a single line of code (but could take a while to find if you don't know what to search for.) Anyway, assume you have something like the following:

   	docs := SelectionInList new.
	docs listHolder: ((UI.AspectAdaptor subject: sys sendsUpdates: true) forAspect: #doctors).

If we wanted to have the 5th doctor selected when the GUI is opened up, we would need to add the following line of code:

 docs selectionIndexHolder value: 5.

The great power of this trick is seen when you are able to generate a particular index based on a current user/"state". For instance, for our project, we wanted a patients' doctor to be selected in the list of doctors when the patient info was being viewed (if the pre-selected doctor was changed, the patient would be assigned a new doctor). The code to handle this task was:

	model doctor isNil ifFalse: [ sys doctors do: [:d | d uid = model doctor uid ifTrue: [idx := (c).] ifFalse: [c := c+1]]. self docs selectionIndexHolder value: idx.].

While this code probably isn't too useful for you in the way that it is presented, the idea of being able to search through an OrderedCollection, to find a desired index, and to properly set the correct index of the List in the GUI should be of some use!

Links to this Page