Does anyone know if it is possible to add other controls to a Flex 3 Alert? What I need is a modal dialoge that allows the user to type in a filename before clicking OK or CANCEL. This seems like it would be best achieved with an Alert but in the documentation I don't see an obvious way to add a TextInput (or any other control except a Button). I'd rather not have to reinvent the wheel but if I have to then hey, I'll do it!
Flex alert with TextInput as well as buttons
actionscript-3apache-flex
Related Solutions
This function creates a pair of textinput/label, where label.text is binded to data in textinput. This should be a good starting point for your code.
private function createTextFieldWithLabel ():void
{
var tf:TextInput = new TextInput();
var label:Label = new Label();
var binding:ChangeWatcher = BindingUtils.bindProperty(label, "text", tf, "text");
var hbox:HBox = new HBox();
hbox.addChild(tf);
hbox.addChild(label);
addChild(hbox);
}
first thanks to dan for his answer. I tried it, and it didn't work.
However, dan's post pointed me to the right direction.
First, to place you in a better context, let me show the main mxml file (SimpleCart.mxml):
<?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"
xmlns:ctrl="controls.*"
xmlns:cart="cart.*"
minWidth="1024" minHeight="768" >
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Style source="SimpleCart.css"/>
<ctrl:NewItem id="buttonAddItem" x="9" y="15" width="394" height="27"/>
<cart:SmartShoppingCart x="8" y="47" width="378"/>
</s:Application>
I think the problem is that the component grouping the Label, TextInput and Button -called NewItem- didn't get the focus, although the TextInput control did.
So, I simply added a call to this.SetFocus, to set the focus to the NewItem component, before setting the focus to the TextInput control.
The source code for the working version of NewItem is this (look for the //Solution comments to find the changes):
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo" >
<fx:Script>
<![CDATA[
protected function newItemHandler():void
{
import cart.ItemAddedEvent;
import flash.external.ExternalInterface;
var e : ItemAddedEvent = new ItemAddedEvent( "->" + textInputItemDescription.text );
textInputItemDescription.text = "";
// *** Solution begins here
this.setFocus();
// *** Solution ends here
textInputItemDescription.setFocus();
textInputItemDescription.setSelection(0,0);
dispatchEvent( e ); // Bubble to parent = true
}
]]>
</fx:Script>
<fx:Metadata>
[Event(name="ItemAddedEvent",type="cart.ItemAddedEvent",bubbles=true)]
</fx:Metadata>
<mx:Label text="Item description:"/>
<s:TextInput id="textInputItemDescription" enter="newItemHandler() "/>
<s:Button click="newItemHandler()" label="Add to cart"/>
</s:HGroup>
Best Answer
I believe you are looking for a flex Title Window. The Alert allows configuring of buttons and text of the buttons. There are some examples of the title window here.