the main view in my application changes it’s data quite frequently, and uses a comboBox as one of it’s controls. Initially I’d just left the comboBox’s width unset and it was happy to resize itself according to the data presented.
However, our “designer” was not happy…
It was decreed that the comboBox should remain at a fixed width (In it’s collapsed state).
The data labels it presents varies considerably in length, and to set it’s fixed width to the longest would a) look stupid in most cases b) require measuring all possible labels before deciding on the width.
So, I decided that I would make the dropdown size itself to the widest label for any one current dataset in it’s dataProvider, at the point at which the dataProvider changes.
I also thought about a dataTip function in a similar fashion to that of the tree control, but decided that the dropdownwidth made more sense since generally speaking labels for our data are of a similar length in any set, so it’s a different question than just the odd one long label in the dropdown.
This proved to be fairly straightforward to get working, though it was new to me and I have to admit I still lack confidence that I’ve done it in entirely the correct way.
However, it does seem to work OK so thats probably good enough until I learn a little more.
Measuring the labels widths was easy (dropdown.measureWidthOfItems) , and initially it seemed to make sense to do that and alter dropdownWidth when the dataprovider changed (in collectionChangeHandler() ) .
However, that lead to a dropdown that was less than the width of the base which was ugly, so I thought I’d “just” do dropdownWidth = Math.max(newDropDownWidth, this.width).
That, of course, turned out to be a problem if the dropDown was visible at the time (since that affects the measured width of the component), and lead to some rather strange results ![]()
Some experimentation showed that the “FlexEvent.VALUE_COMMIT” event occurred at a nice time to measure and set what the new dropdownwidth should be (The dropdown is hidden before the event occurs, so you get the “base” width at that point. On reflection, it may have been worth getting the width of the “base” element somehow, but I didn’t look for that.
The other thing that needed attention was that “measureWidthOfItems” does not take into account the presence or otherwise of the scrollbar on the dropdown.
I detected it’s presence by using dropdown.maxVerticalScrollPosition > 0, but initially hardcoded it’s width to the standard 16 pixels(it was late at the time).
There’s no direct way to get the scrollbars width (it’s protected within the standard dropdown), instead you must extend your dropdown with a method that reads and returns it.
Though I’d not done that before, it proved to be easy enough. However, I’m slightly suspicious that the fact I use a cast to get my extended dropdown class before being able to call the scrollbar width method means that I’ve not quite got it 100% right. But considering it only gets called when the dataProvider changes, I’m not too bothered in practice…
All that remains is to make an example featuring cute puppies…
View example (right click example for source)


