Flex Tab Key Navigation for custom Spark PopUp

October 6, 2010. Posted by Andy Hulstkamp under Actionscript 3 flex 4

This took me a while to figure out.

In a project I created a custom PopUp, nothing fancy, just a couple of text inputs. When I tried to enable Tab key navigation I set the required properties but Flex would simply ignore tab key navigation to the input fields in the pop up.

The Documentation about the IFocusManager states that a pop up will get its own FocusManager and its own tab loop.

After some tracing I found that my pop up and its child weren’t included in the FocusManager as focus objects. In fact, there was no FocusManager for my pop up at all.

The reason for this is that when the PopUpMangerImpl is about to create and configure the pop up, it checks if the pop up is a IFocusManagerContainer so that focus can be added.

Unfortunately my popup was a Group which does not implement the IFocusManagerContainer interface. Naturally no FocusManager would be created.

If you want to use a Spark Group as your pop up, the solution is to create a new class that extends from Group and implements the IFocusManagerContainer. Then use this class as the container for the pop up.

.
.
.
public class AHPopUpGroup extends Group implements IFocusManagerContainer
	{
 
		public function AHPopUpGroup()
		{
			super();
		}
 
		/**
		 *  @private
		 *  Storage for the defaultButton property.
		 */
		private var _defaultButton:IFlexDisplayObject;
 
		[Inspectable(category="General")]
 
		/**
		 *  The Button control designated as the default button for the container.
		 *  When controls in the container have focus, pressing the
		 *  Enter key is the same as clicking this Button control.
		 *
		 *  @default null
		 */
		public function get defaultButton():IFlexDisplayObject
		{
			return _defaultButton;
		}
 
		/**
		 *  @private
		 */
		public function set defaultButton(value:IFlexDisplayObject):void
		{
			_defaultButton = value;
			ContainerGlobals.focusedContainer = null;
		}
 
	}
.
.
.

And the mxml for the pop up becomes something like this

.
.
.
 
		<!---
		Uses a Group that implements the IFocusManagerContainer
		-->
 
			.
			.
			.
			TextInput, Buttons etc now get focus on tab key navigation
			.
			.
			.
 
.
.
.


Share/Save/Bookmark

5 Responses to “Flex Tab Key Navigation for custom Spark PopUp”

  1. Florian Says:

    Thanks for the tip!

    I already know it will save me some headache….

  2. Adam Says:

    All you have to do is make your popup a SkinnableContainer instead of Group, since SkinnableContainer implements IFocusManagerContainer.

  3. Andy Hulstkamp Says:

    Thanks for the tip, Adam. That’s right but I’d rather do not have the additional overhead of the SkinnableContainer when I don’t need it.

  4. Aghosh Babu Says:

    I had been trying to figure this out for a whole darn day now, reposting this in my blog!

    Also, what about states implementation ?

  5. Alex Says:

    Thanks mate. Really saved me some time.