Remember back in the Flex 3 days where we had to set verticalScrollPolicy/horizontalScrollPolicy to off all the time? Well, no more of that. About the only thing that gets scrollBars automatically is the TextArea. You want scrollBars somewhere else then you have to use the <s:Scroller> class.
If you would like to make your Flex application scrollable and put a border around it then all you have to do is skin the application by setting its skinClass like so:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
width="300" height="300"
skinClass="skins.AppSkin" >
| view source |
Your AppSkin is simple. Just throw in a <s:Scroller> around the <s:Group id=”contentGroup”> and then use FXG to throw a border around the whole mess and you can call it a day.
<s:Skin
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
alpha.disabled="0.5">
<fx:Metadata>
[HostComponent("spark.components.Application")]
</fx:Metadata>
<s:states>
<s:State name="normal" />
<s:State name="disabled" />
</s:states>
<s:Scroller
id="scroller" width="100%" height="100%"
hasFocusableChildren="true"
verticalScrollPolicy="auto"
horizontalScrollPolicy="auto">
<s:Group id="contentGroup" left="0" right="0" top="0" bottom="0" />
</s:Scroller>
<!--
Strokes are centered on the described shape.
So a width of 4 means we have to move the Rect 2 pixels
x and y and then make the Rect width/height 4 pixels less
than the actual width/height of the area we want to encompass.
Plus, we have to take into account the dimensions of the scrollbars.-->
<s:Rect x="1" y="1"
width="{ hostComponent.width - scroller.verticalScrollBar.width - 2 }"
height="{ hostComponent.height - scroller.horizontalScrollBar.height - 2 }">
<s:stroke>
<s:SolidColorStroke color="#000000" alpha="0.5" weight="2" />
</s:stroke>
</s:Rect>
</s:Skin>
That’s all.





I've noticed that sometimes the mouse scroll wheel works in Flex 4 Scrollers and sometimes it doesn't. It doesn't seem to be working for me in your demo. Any ideas on that?
@James Ward: Yes, I have noticed that. And when it does work it’s verrrrry slow. Almost like it moving a few pixels at a time. I actually published this post first because I have sort of manually bypassed the auto-scrolling and made my own. I’ll publish it later tonight.
@ James Ward
If you are on Mac, then it's a known bug in Flash Player (even on v.10; I haven't check 10.1) – mouse wheel doesn't work on Mac.
I believe it is filed, but hasn't been fixed yet.
FQ
@FTQuest, My example isn’t working on Windows either. When I use the scroll wheel it scrolls the page. Even if I give the SWF focus. It still scrolls the page. :(
I really enjoyed your article, I tweet!
This is My First Visit to your Blog.. I am an Engineering student and I have Flex-2 as a subject! i dunno when i will be completing flex-2 and will start with flex-4!
Nice scrollable app.. infact this is my first ever SEEN flex app!
Thanks
Chethan
http://chaaps.com
@Chethan, They are teaching Flex 2 in college. Wow, way to be 3 years behind the times! That really sucks. At least the core concepts don’t change: Databinding, MXML, Actionscript – although databinding has improved with Flex 4 it still works the same. My suggestion is that you should jump on Flex 4 as soon as the class is over if you think it’s something that you’ll want to continue doing.
Yes, I have noticed that. And when it does work it's verrrrry slow. Almost like it moving a few pixels at a time. I actually published this post first because I have sort of manually bypassed the auto-scrolling and made my own. I'll publish it later tonight.
btw: flex 4 apps still scroll automatically. Is there a way to turn this off?