Sometimes I wanted to override a function of Flex component, but it was private. So what to do? If I need just a little change, or I just found a little bug there, and don’t want to make a component from scratch! Let’s go little more extreme.
I tried to make a new class with the code of the component, but it it was mistake. There were always some base classes of the actual compnents, and those can mess things up really quickly. The problem is usually with the baseclasses somewhere, and usually just on runtime. Like cannot cast something to something (because of the different name of my new class) etc. So good luck with this.
The better way is to copy the frameworks class to your source folder, and make changes in that. This way you can make changes to some really base classes too,not just what you want to extend. You will need to maintain the package structure, without that you would never know which version you actually use. Flex builder will prefer the classes from your source folder over the swc of the framework. So the one in your source folder will be used to compile this project. I think it may slow down the compilation, not sure. If later the sdk will be changed and these classes will not be updated, it can be a problem.
The flex framework source folder on my pc is in (we want to the skd-3): c:\Program Files\Adobe\Flex Builder3\sdks\3.0.0\frameworks\projects\framework\src\mx\
There is a notice in that folder:
All of the files contained in this directory and any subdirectories are considered “SDK Source Files” under the terms of the end user license agreement that accompanies this product. Please consult such end user license agreement for details about your rights with respect to such files.
So I don’t really know if I am allowed to make these changes, but I will show you how to do them. (It would be a lot easier to write: You cannot use these files! You can use these files!) Sometimes you will need to use them.
There can be several files included in the class, you will have to add those to the source folder too. Many classes include some code, you will need them to compile the code.
There can be problems with the Flex builder, it may keep delete the next lines of code if Flex is set to manage imports. Check the original files where these lines have to go.
import mx.core.mx_internal; use namespace mx_internal;
Just be aware of this.
Now you can start!
The best thing to do is just to change private functions to protected ones. There is not big risk. Then you can extend the class and override the functions. Whatever you want. Theoretically you should do always this, make changes to the base class to allow more changes, but use it’s extended version in your code. Problems can for example that the function should be called after something else was set, and you will mess things up. This can happen with variables too, the components will be in inconsistent state after you set some variable by your own way. It’s your own risk.
Sometimes this is not enough, you have to make change in the code. It’s really important to not change the default behavior of the class. If you change something it will affect it’s every instance. Other developers in your team (working on the same project) may not really like what you did. Because you probably use SVN everybody will have the same changed version of the basic class, with the same changed functionality. So be avare what are you doing! You can put some conditions, to not change the default behavior. Make sure the functionality didn’t change! The changed class may be copied, and used in different project by different people, they will go mad.
I decided to divide this post. To Datagrid ColumnWidht with fixed ratio! and The white rectangle attacks!
sources for those posts:
OverrideComponents.zip
TheWhiteRectangleAttacks.zip

Friday, 10. July 2009
I found this post because I needed to overload a private method… But, alas, the method you talk about here didn’t work for me (I don’t have access to the source code). I finally ended up hacking the byte code, which I’ve written about here: http://blog.codekills.net/archives/56-Overriding-private-methods-in-ActionScript,-the-hard-way.html