Extending the LinkButton control in Flex
The following example shows how you can extend the Flex LinkButton control and add your own custom styles to a custom skin class.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/08/extending-the-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_skin_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:comps="comps.*"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="enabled:">
<mx:CheckBox id="checkBox" selected="true" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<comps:CustomLinkButton1 id="linkButtonMXML"
label="LinkButton (MXML)"
toggle="true"
enabled="{checkBox.selected}"
skin="skins.CustomLinkButtonSkin1"
rollOverColor="red"
selectionColor="haloOrange"
toggleBackgroundColor="yellow" />
<comps:CustomLinkButton2 id="linkButtonAS"
label="LinkButton (ActionScript)"
toggle="true"
enabled="{checkBox.selected}"
skin="skins.CustomLinkButtonSkin1"
rollOverColor="red"
selectionColor="haloOrange"
toggleBackgroundColor="yellow" />
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/08/extending-the-linkbutton-control-in-flex/ -->
<mx:LinkButton xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Metadata>
[Style(name="toggleBackgroundColor",
type="uint",
format="Color",
inherit="yes")]
</mx:Metadata>
<mx:Script>
<![CDATA[
override public function set enabled(value:Boolean):void {
super.enabled = value;
useHandCursor = value;
}
]]>
</mx:Script>
</mx:LinkButton>
/**
* http://blog.flexexamples.com/2008/09/08/extending-the-linkbutton-control-in-flex/
*/
package comps {
import mx.controls.LinkButton;
[Style(name="toggleBackgroundColor",
type="uint",
format="Color",
inherit="yes")]
public class CustomLinkButton2 extends LinkButton {
public function CustomLinkButton2() {
super();
}
override public function set enabled(value:Boolean):void {
super.enabled = value;
useHandCursor = value;
}
}
}
skins/CustomLinkButtonSkin1.as
/**
* http://blog.flexexamples.com/2008/09/08/extending-the-linkbutton-control-in-flex/
*/
package skins {
import mx.skins.halo.LinkButtonSkin;
import mx.styles.StyleManager;
public class CustomLinkButtonSkin1 extends LinkButtonSkin {
public function CustomLinkButtonSkin1() {
super();
}
override protected function updateDisplayList(w:Number, h:Number):void {
super.updateDisplayList(w, h);
// Inherited styles
var cornerRadius:Number = getStyle("cornerRadius");
var rollOverColor:uint = getStyle("rollOverColor");
var selectionColor:uint = getStyle("selectionColor");
// Custom styles
var toggleBackgroundColor:uint = getStyle("toggleBackgroundColor") || getStyle("themeColor");
graphics.clear();
switch (name) {
case "upSkin":
// Draw invisible shape so we have a hit area.
drawRoundRect(
0, /* x */
0, /* y */
w, /* width */
h, /* height */
cornerRadius, /* cornerRadius */
0, /* color */
0.0 /* alpha */
);
break;
case "selectedUpSkin":
case "selectedOverSkin":
drawRoundRect(0, 0, w, h, cornerRadius, toggleBackgroundColor, 1.0);
break;
case "overSkin":
drawRoundRect(0, 0, w, h, cornerRadius, rollOverColor, 1.0);
break;
case "selectedDownSkin":
case "downSkin":
drawRoundRect(0, 0, w, h, cornerRadius, selectionColor, 1.0);
break;
case "selectedDisabledSkin":
// Draw 20% alpha shape so we have a hit area.
drawRoundRect(0, 0, w, h, cornerRadius, toggleBackgroundColor, 0.2);
break;
case "disabledSkin":
// Draw invisible shape so we have a hit area.
drawRoundRect( 0, 0, w, h, cornerRadius, 0, 0.0);
break;
}
}
}
}
View source is enabled in the following example.
You can also specify the custom skin class in an external .CSS file or <mx:Style /> block, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/08/extending-the-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_skin_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:comps="comps.*"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
CustomLinkButton1 {
skin: ClassReference("skins.CustomLinkButtonSkin1");
}
</mx:Style>
<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="enabled:">
<mx:CheckBox id="checkBox" selected="true" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<comps:CustomLinkButton1 id="linkButton"
label="LinkButton"
toggle="true"
enabled="{checkBox.selected}"
rollOverColor="red"
selectionColor="haloOrange"
toggleBackgroundColor="yellow" />
</mx:Application>
Or, you can set the skin style using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/08/extending-the-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_skin_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
initialize="init();">
<mx:Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
import comps.CustomLinkButton1;
import skins.CustomLinkButtonSkin1;
private var linkButton:CustomLinkButton1;
private function init():void {
linkButton = new CustomLinkButton1();
linkButton.label = "LinkButton";
linkButton.toggle = true;
linkButton.setStyle("skin", CustomLinkButtonSkin1);
linkButton.setStyle("rollOverColor", "red");
linkButton.setStyle("selectionColor", "haloOrange");
linkButton.setStyle("toggleBackgroundColor", "yellow");
addChild(linkButton);
BindingUtils.bindProperty(linkButton, "enabled",
checkBox, "selected");
}
]]>
</mx:Script>
<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="enabled:">
<mx:CheckBox id="checkBox" selected="true" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
</mx:Application>
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.
9 Responses to Extending the LinkButton control in Flex
Leave a Reply Cancel reply
-
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


Hello,
Can you give an example about a secure authentification method for flex, I heard something about using Spring Acegi on Google, but didn’t seen a good example.
I’m trying to follow this, however I’m having a slight problem trying to streamline this into one mxml where I have (in order):
ButtonScrollingCanvas
HBox
Repeater
LinkButton (label, toolTip, name and click(event) are all handled here)
/Repeater
/HBox
/ButtonScrollingCanvas
What I want to do is once a linkbutton is clicked, it should stay highlight until the next one is clicked. And this method, while darn close, isn’t working for me exactly.
Thanks, this makes sense, but I want to take it one step further and put the custom linkButton in a linkBar. Can you explain how to do that, or point me to something that does? I haven’t found anything. Thanks!
Hi, good sample. It there way to hide rollover background at all?
Thanks
Ruslan,
Yes. If you want to remove the rollover color you can either set the
skinstyle tonull, or extend the LinkButtonSkin class (mx.skins.halo.LinkButtonSkin.as) and set the alpha to 0% in each skin state.And then my custom skin, TransparentLinkButtonSkin.as, looks like this:
package { import mx.skins.halo.LinkButtonSkin; public class TransparentLinkButtonSkin extends LinkButtonSkin { public function TransparentLinkButtonSkin() { super(); } // END function TransparentLinkButtonSkin(); override protected function updateDisplayList(w:Number, h:Number):void { super.updateDisplayList(w, h); var cornerRadius:Number = getStyle("cornerRadius"); graphics.clear(); switch (name) { case "upSkin": case "overSkin": case "downSkin": case "disabledSkin": // Draw invisible shape so we have a hit area. drawRoundRect( 0, 0, w, h, cornerRadius, 0, 0); break; } // END switch; } // END function updateDisplayList(); } // END class TransparentLinkButtonSkin; } // END package;Peter
I recently had a customer who was trying to debug his Flex components. Under certain circumstances the updateDisplayList() method of his component was getting called and it wasn’t obvious why.
For some reason my rollover isn’t working. Nothing happens? The selection works fine. Also is there a way to change the font color as well?
Rollover working, but can’t seem to change the text color?
@jason,
Try a combination of the
color,textRollOverColor, andtextSelectedColorstyles:There should be a few random examples at http://blog.flexexamples.com/?tag=textRollOverColor,textSelectedColor, although nothing specific to LinkButton. This may help also: http://blog.flexexamples.com/2007/08/26/changing-a-button-controls-text-color/
Peter