CheckBoxGroup 2.0 (Beta)

This post was written by jimrobson on December 23, 2008
Posted Under: Flex

About 15 months ago I released the initial version of CheckBoxGroup, and I was very happy to see that a number of people found it useful. However, like most developers, I can’t stay happy with my creations for very long; I always seem to think of things that I could do to extend and/or improve them. So it is with the CheckBoxGroup: although it was a decent and useful control as it was, I really wanted to make it better. (If you’re not familiar with what the CheckBoxGroup does, take a quick look at the original post.)

The primary issue that I wanted to address was that it could not be implemented in MXML; the only way to instantiate it was in ActionScript. This was not ideal, especially considering that there are many developers who prefer to work as much as possible in MXML. So the biggest change for version 2 is that it can be instantiated with an MXML tag.

Of course, in order for a class to be instantiated in MXML it must be a subclass of UIComponent. The Adobe engineers got around this with RadioButtonGroup, as I understand it, by tweaking the compiler so that it made an exception for that particular control. I am not interested in messing with the compiler, so I made CheckBoxGroup a subclass of UIComponent. This may be unfortunate, since it’s not a visible control, but I do not think it is a major issue. First, I don’t foresee any application having so many instances of the control that the additional resources used will be significant. Second, I set visible and includeInLayout to false so that it doesn’t interfere with the appearance of the UI.

In the process of making it MXML-ready, I also created some public properties and methods that make the control more dynamic and flexible. For example, you can now specify the master, subordinate, and other/none-of-the-above checkboxes at any time after instantiation. Of course, you can still pass them to the constructor as you did in version one; this version is fully backward-compatible.

Another new property that I think is very useful is selectedItems. As you would expect, it holds an array of the checkboxes in the group that are selected.

Two useful new methods are addSubordinate() and removeSubordinate(). Just like the names sound, these allow you to add and remove individual subordinate checkboxes at any time.

One caveat: Unlike version 1, this new version has not yet been implemented in a production application. So exercise due diligence. And please - let me know if you find any bugs or have any suggestions for improvement.

Click here or on the image above to see a small sample application that demonstrates how the CheckBoxGroup works. You can right-click on the application or click here to view and download the source code.

The demo application shows three different implementation styles, one ActionScript and two MXML. As far as I can see, which one you choose is purely a matter of personal taste. The demo has a fourth screen that lets you see how the CheckBoxGroup instance behaves as you add and remove individual CheckBoxes.

Enjoy - and again, contact me with any feedback.

Reader Comments

Thank you very much for posting this component.

#1 
Written By Naveen Booma on December 7th, 2009 @ 9:47 am

Add a Comment

required, use real name
required, will not be published
optional, your blog address

Previous Post: Just like old times