11
Sep
07

Customing the Flex HSlider and VSlider controls

The following example shows how you can customize several properties and styles in the Flex HSlider control.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/11/customing-the-flex-hslider-and-vslider-controls/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Style>
        .MyDataTip {
            backgroundAlpha: 1.0;
            backgroundColor: haloBlue;
            color: white;
            fontWeight: bold;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            private function init():void {
                slider.labels = [slider.minimum, slider.maximum];
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form>
            <mx:FormItem label="allowTrackClick:">
                <mx:CheckBox id="allowTrackClickCh" selected="true" />
            </mx:FormItem>
            <mx:FormItem label="invertThumbDirection:">
                <mx:CheckBox id="invertThumbDirectionCh" />
            </mx:FormItem>
            <mx:FormItem label="showTrackHighlight:">
                <mx:CheckBox id="showTrackHighlightCh"
                        selected="true" />
            </mx:FormItem>
            <mx:FormItem label="showDataTip:">
                <mx:CheckBox id="showDataTipCh"
                        selected="true" />
            </mx:FormItem>
            <mx:FormItem label="dataTipPlacement:">
                <mx:ComboBox id="dataTipPlacementCB"
                        selectedIndex="2">
                    <mx:dataProvider>
                        <mx:Array>
                            <mx:Object label="left" />
                            <mx:Object label="right" />
                            <mx:Object label="top" />
                            <mx:Object label="bottom" />
                        </mx:Array>
                    </mx:dataProvider>
                </mx:ComboBox>
            </mx:FormItem>
            <mx:FormItem label="dataTipPrecision:">
                <mx:NumericStepper id="dataTipPrecisionNS"
                        minimum="0"
                        maximum="3"
                        value="2" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:HSlider id="slider"
            invertThumbDirection="{invertThumbDirectionCh.selected}"
            minimum="-10"
            maximum="20"
            tickInterval="5"
            liveDragging="true"
            showDataTip="{showDataTipCh.selected}"
            showTrackHighlight="{showTrackHighlightCh.selected}"
            dataTipPlacement="{dataTipPlacementCB.selectedItem.label}"
            allowTrackClick="{allowTrackClickCh.selected}"
            dataTipPrecision="{dataTipPrecisionNS.value}"
            dataTipStyleName="MyDataTip" />

</mx:Application>

View source is enabled in the following example.


12 Responses to “Customing the Flex HSlider and VSlider controls”


  1. 1 Aasim Sep 12th, 2007 at 8:41 am

    Can we change the track height for the HSlider?

  2. 2 peterd Sep 12th, 2007 at 9:23 am

    Aasim,

    I don’t believe there is a style/property specifically for track height. You can probably just create a custom skin though. If you think it is a bug, or want to file an enhancement request, you can go to http://bugs.adobe.com/flex/ and submit it to the official Flex bug base.

    Peter

  3. 3 Aasim Sep 13th, 2007 at 4:46 am

    Thanks Peter,

    just another question…
    a hslider track bar has a small offset of around 3 pixels from the left and the right side, can this be reduced?

  4. 4 peterd Sep 13th, 2007 at 6:58 am

    Aasim,

    Not sure. I believe the Slider class (mx.controls.sliderClasses.Slider) has a “thumbOffset” style which may be what you are looking for. If not, you may have to try asking on Flexcoders and see if somebody on there knows.

    Sorry,

    Peter

  5. 5 Pedro Sep 13th, 2007 at 7:23 am

    Is there a way to change the order of the min max positions on the slider so that the max value is on the left and the min value on the right?

  6. 6 peterd Sep 13th, 2007 at 10:03 pm

    Pedro,

    There isn’t a way I know of, although you could probably extend the Slider (or HSlider/VSlider) class and add the functionality. Either that or just use the labelFunction and handle all the value conversions behind the scenes manually. You could always try asking on the Flexcoders list, I’m sure somebody out there has attempted this before.

    Sorry,
    Peter

    Note: These questions always work better if I already know the answers. Otherwise I just look kind of silly. ;)

  7. 7 Anand Jha Mar 8th, 2008 at 2:40 am

    Hi Peter

    Please help me in achieving the following:

    “To drag the region between the thumbs, if the HSlider has two thumbs”.

    Do we have any option in HSlider?

    Or

    Do i need to rewrite HSlider which will implement Slider?

    Thanx in advance

    ~Anand

  8. 8 peterd Mar 8th, 2008 at 10:18 am

    Anand,

    Something like Doug McCune’s Draggable Slider Component for Flex?

    Peter

  9. 9 Erich Beyrent Mar 19th, 2008 at 1:28 pm

    I know there is a way to set the toolTip for the slider, but do you know of a way to override the dataTip to display something else?

    I’m using the DisplayShelf component to display images, and as the slider moves, I want to update the dataTip with the name or caption of the image.

    Any ideas?

  10. 10 peterd Mar 19th, 2008 at 1:49 pm
  11. 11 Erich Beyrent Mar 20th, 2008 at 5:24 am

    Peter,

    The first link had exactly what I needed - thank you!!!

    -Erich-

  12. 12 Shameer Salim Jun 2nd, 2008 at 7:59 am

    Hi,..
    Is it possible to rotate the silder label text??

    I tried with applying styles like
    HSlider
    {
    label-rotation:90;
    }
    and
    HSlider
    {
    label-style-name:mySliderStyle;
    }
    .mySliderStyle
    {
    rotation:90;
    }

    But both of them didnt work

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".