Adding Flex Components to Native Windows in AIR – Don’t Make the Mistake I Did

I was playing around with the NativeWindow API the past couple of days and I ran into a roadblock. With Native Windows you create them and then you’re supposed to be able to add stuff to the display list using the addChild() method. But when I tried to add any kind of Flex Component, it wouldn’t work (reading the docs more closely confirmed that). Thanks to Sasha Magee from the engineering team I know why, and it’s pretty obvious.

The NativeWindow class doesn’t support all of the “Flex stuff” out of the box. It doesn’t know how to handle popping up a combo box, how to layout components or any of the “Flex smarts” as Sasha calls it. So when you try to add a component to the NativeWindow you’ve just created, it won’t work.

What I had to do was change the base class of my custom component from mx:Canvas to mx:Window. Now in order to create a window I just create a new instance of that component (in this case WaypointEditor), set the height and width, then call the open() method:

[code]
var wptEditor : WaypointEditor = new WaypointEditor();
wptEditor.height = 500;
wptEditor.width = 500;
wptEditor.open();
[code]

One other thing I noticed is that when doing Native Windows you can't call the acvitate() method that the documents use. You have to call open() in order to pop it up. Once you've created the window with the base class of mx:Window you can add any other Flex components to the display list. So if you don't want to have to change your custom component to mx:Window you can just create a custom component with mx:Window and nothing else, then create that and add your other custom component to the window using addChild().

[tags]Flex, AIR, NativeWindow[/tags]

  • http://blog.everythingflex.com/wp-content/uploads/2007/12/flashon.jpg Rich Tretola

    This is a pretty common issue and it stems from the fact that the original Apollo alpha didn’t contain the mx:Window component. So many developers including myself starting using NativeWindow along with a few hacks to get native Flex components into an AIR window. With the addition of the mx:Window, the use of NativeWindow within Flex based AIR applications is no longer necessary.

  • http://userflex.wordpress.com/ Nick

    I think that every time this happens, somewhere deep inside Adobe, in the farthest reaches of some dark corner office, Mike Chambers is laughing. :-)

  • http://www.gaimtheory.com Dustin

    I ran into this exact issue. Thanks for the explanation

  • Johan Nyberg

    Thanks! I know this saved me a lot of headaches.

  • http://www.andymatthews.net andy matthews

    This helped out a lot Ryan. Appreciated.

  • @jacsdev

    Thanks buddy, it’s a great explanation .. i really apreciate it

  • John Travis

    I think that the NativeWindow has it’s part to play as you can do stuff that designers like (well, the designers I worth with anyway) like transparency, non-standard shaped windows, etc. You can also keep them out of the Windows taskbar.

    There is another way around this – just sub-class the NativeWindow class so that it creates a system manager. This is the missing piece that stops MX controls working properly on a NativeWindow.

    There’s a sample that explains how to do it here:

    http://www.ben-morris.com/howto-add-flex-mx-controls-to-a-nativewindow-for-adobe-air