Setting the width of the dropdown menu on a Spark DropDownList control in Flex 4
In a previous example, “Setting the width of the dropdown menu on a ComboBox control in Flex”, we saw how you could set the width of the dropdown menu on a Flex MX ComboBox control by setting the dropdownWidth property.
The following example shows how you can change the width of the Spark DropDownList control’s pop up menu so the pop up menu width matches the contents rather than the DropDownMenu button control’s width by setting the Boolean popUpWidthMatchesAnchorWidth property.
The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2010/01/28/setting-the-width-of-the-dropdown-menu-on-a-spark-dropdownlist-control-in-flex-4/ --> <s:Application name="Spark_DropDownList_PopUpAnchor_popUpWidthMatchesAnchorWidth_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"> <s:DropDownList id="list" labelField="name" requireSelection="true" skinClass="skins.CustomDropDownListSkin" width="120" horizontalCenter="0" top="20"> <s:dataProvider> <s:ArrayList> <fx:Object name="Adobe Illustrator CS5" /> <fx:Object name="Adobe AIR 2.0" /> <fx:Object name="ColdFusion 9" /> <fx:Object name="Dreamweaver CS5" /> <fx:Object name="Flash Professional CS5" /> <fx:Object name="Adobe Flash Player 10.1" /> <fx:Object name="Fireworks CS5" /> <fx:Object name="Flex 4.0" /> <fx:Object name="Lightroom 2.0" /> <fx:Object name="Photoshop CS5" /> </s:ArrayList> </s:dataProvider> </s:DropDownList> </s:Application>
Then in the custom DropDownList skin you need to set the popUpWidthMatchesAnchorWidth property to false, as seen in the following snippet:
<s:PopUpAnchor id="popUp" displayPopUp.normal="false" displayPopUp.open="true" includeIn="open" left="0" right="0" top="0" bottom="0" itemDestructionPolicy="auto" popUpPosition="below" popUpWidthMatchesAnchorWidth="false">
View source is enabled in the following example.
The full custom DropDownList skin, skins/CustomDropDownListSkin.mxml, is as follows:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2010/01/28/setting-the-width-of-the-dropdown-menu-on-a-spark-dropdownlist-control-in-flex-4/ --> <s:SparkSkin name="CustomDropDownListSkin" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:fb="http://ns.adobe.com/flashbuilder/2009" alpha.disabled="0.5"> <!-- states --> <s:states> <s:State name="normal" /> <s:State name="open" /> <s:State name="disabled" /> </s:states> <!-- host component --> <fx:Metadata> <![CDATA[ [HostComponent("spark.components.DropDownList")] ]]> </fx:Metadata> <fx:Script fb:purpose="styling"> <![CDATA[ import mx.events.FlexEvent; /* Define the content fill items that should be colored by the "contentBackgroundColor" style. */ static private const contentFill:Array = ["bgFill"]; override public function get contentItems():Array {return contentFill}; override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { if (getStyle("borderVisible") == false) { if (border) { border.visible = false; } if (background) { background.left = background.top = background.right = background.bottom = 0; } if (scroller) { scroller.minViewportInset = 0; } } else { if (border) { border.visible = true; } if (background) { background.left = background.top = background.right = background.bottom = 1; } if (scroller) { scroller.minViewportInset = 1; } } if (dropShadow) { dropShadow.visible = getStyle("dropShadowVisible"); } openButton.setStyle("cornerRadius", getStyle("cornerRadius")); if (borderStroke) { borderStroke.color = getStyle("borderColor"); borderStroke.alpha = getStyle("borderAlpha"); } super.updateDisplayList(unscaledWidth, unscaledHeight); } protected function labelDisplay_updateCompleteHandler(evt:FlexEvent):void { hostComponent.toolTip = labelDisplay.toolTip; } ]]> </fx:Script> <!--- The PopUpAnchor control that opens the drop-down list. <p>In a custom skin class that uses transitions, set the <code>itemDestructionPolicy</code> property to <code>none</code>.</p> --> <s:PopUpAnchor id="popUp" displayPopUp.normal="false" displayPopUp.open="true" includeIn="open" left="0" right="0" top="0" bottom="0" itemDestructionPolicy="auto" popUpPosition="below" popUpWidthMatchesAnchorWidth="false"> <!--- This includes borders, background colors, scrollers, and filters. --> <s:Group id="dropDown" maxHeight="134" minHeight="22"> <s:RectangularDropShadow id="dropShadow" blurX="20" blurY="20" alpha="0.45" distance="7" angle="90" color="#000000" left="0" right="0" top="0" bottom="0" /> <s:Rect id="border" left="0" right="0" top="0" bottom="0"> <s:stroke> <s:SolidColorStroke id="borderStroke" weight="1" /> </s:stroke> </s:Rect> <!-- fill --> <!--- Defines the appearance of drop-down list's background fill. --> <s:Rect id="background" left="1" right="1" top="1" bottom="1" > <s:fill> <!--- The color of the drop down's background fill. The default color is 0xFFFFFF. --> <s:SolidColor id="bgFill" color="0xFFFFFF" /> </s:fill> </s:Rect> <s:Scroller id="scroller" left="0" top="0" right="0" bottom="0" hasFocusableChildren="false" minViewportInset="1"> <s:DataGroup id="dataGroup" itemRenderer="spark.skins.spark.DefaultItemRenderer"> <s:layout> <s:VerticalLayout gap="0" horizontalAlign="contentJustify"/> </s:layout> </s:DataGroup> </s:Scroller> </s:Group> </s:PopUpAnchor> <!--- The default skin is DropDownListButtonSkin. --> <s:Button id="openButton" skinClass="spark.skins.spark.DropDownListButtonSkin" focusEnabled="false" left="0" right="0" top="0" bottom="0" /> <s:Label id="labelDisplay" maxDisplayedLines="1" mouseEnabled="false" mouseChildren="false" left="7" right="30" top="2" bottom="2" width="75" verticalAlign="middle" verticalCenter="1" showTruncationTip="true" updateComplete="labelDisplay_updateCompleteHandler(event);" /> </s:SparkSkin>
This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
10 Responses to Setting the width of the dropdown menu on a Spark DropDownList control in Flex 4
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Thanks for the example, but I have a question:
At this moment I have a custom dropDownSkin for that is used by all my dropdowns.
Some of the dropdowns I want to give a popup with another width so I changed the width property of the group of the PopUpAnchor to a specific width.
But from now one, all my dropdowns use that width. Can I make this dynamic? For example by adding a style property to the skin that I can use in my mxml components?
Or do I really have to make a skin for each drop down?
Thanks for your time!
The way I do it now is like they explain in this tutorial: http://tv.adobe.com/watch/flex-in-a-week-day-5/creating-custom-skin-properties/
I hoped for a better solution, but this is the best I can find till now!
@Wannes,
You could try something like this, setting the DropDownList control’s drop down width using the
openevent:Peter
Hi Peter – how would I display a toolTip on the items _in_the_dropDownList? (i.e., not just the selectedItem)
thanks
i need this code in flash builder 4 with the current sdk. what are the changes needed?
thanx
@Saariko,
I just tested the code above with Flex Hero (4.5) beta and the code still worked as expected.
Which version of the Flex SDK are you using, and which issues are you seeing?
Peter
Hi, when the first item of my dataprovider is very small (3 letters) compared with those of more, the width of the dropdown dont work, the others items are displayed to the half
any suggestions ?
@ one1,
I’ve been having the same problems. I’ve been working through either defining all of the items in the list and getting the widest item and setting the dropdown width to that (which is slow and not optimized), or trying to set the width of the item renderer when the label gets set. Unfortunately, with the 2nd method I end up getting multi-line labels, and if I try to cancel that by setting the maxLinesToDisplay, the content width gets clipped again :(
Is this a bug? Peter, have you seen this behaviour before?
can I have the toolTip on the focusedItem of the dropDownList..?
further to this to be able to set the popup (open) state to match both the height and the width of the displayed items the following works nicely