Flex FormItem layout in nested component - apache-flex

I have a fairly straightforward Flex layout question.
Is there anyway to get FormItem's contained within a nested container to follow the alignment of the FormItem's in in the parent form container?
For example:
<mx:Form>
<mx:FormItem label="This is a long label" id="formItem1">
<mx:HBox>
<mx:TextInput />
</mx:HBox>
</mx:FormItem>
<s:BorderContainer>
<mx:FormItem label="ShrtLbl" id="formItem2">
<mx:HBox>
<mx:TextInput />
</mx:HBox>
</mx:FormItem>
</s:BorderContainer>
</mx:Form>
In this case I would like the label for both formItem1 and formItem2 to have to the same width which would be the case if defined as follows:
<mx:Form>
<mx:FormItem label="This is a long label" id="formItem1">
<mx:HBox>
<mx:TextInput />
</mx:HBox>
</mx:FormItem>
<mx:FormItem label="ShrtLbl" id="formItem2">
<mx:HBox>
<mx:TextInput />
</mx:HBox>
</mx:FormItem>
</mx:Form>
Any thoughts?

The Spark Form Skin uses the spark.layouts.FormLayout to control the layout of its children.
From the FormLayout source:
The FormLayout class defines the default layout for Spark Form skins.
FormLayout provides a vertical layout for the child FormItem containers in the Form.
If any of the child containers uses a FormItemLayout, FormLayout will
align the ConstraintColumns of each child.
Therefore the Layout does not look at any nested children of the form.
I would recommend creating your own custom form layout to handle your case.
oops, I see you're using the mx:FormItem not the s:FormItem. I would recommend using the spark variants.
Furthermore, there is no need to have an mx:HBox (or s:HGroup) inside a FormItem.

Related

Flex: Place mx:FormItems side by side in a mx:Form

I have a mx:Form containing two FormItems. By default the FormItems are placed vertically one on top of the other but due to space constraints I want to place the two FormItems side by side. How can I do it? Another option that I had was to use just the plain label and text fields but I want to make use of the features that the mx:Form container offers.
I have the following code that places the data vertically:
<mx:HBox width="100%"
horizontalAlign="center">
<mx:Label text="Info"/>
</mx:HBox>
<mx:Form id="InfoForm">
<mx:FormItem label="Info1"
horizontalAlign="center"
paddingLeft="45"
required="true"
direction="vertical">
<mx:TextInput id="Info1TextInput"/>
</mx:FormItem>
<mx:FormItem label="Info2"
horizontalAlign="center"
paddingLeft="45"
required="true"
direction="vertical">
<mx:TextInput id="Info2TextInput"/>
</mx:FormItem>
</mx:Form>
Thanks a lot!
Well, how I achieved it using 2 Forms. side by side in horizontal layout. Hope this helps.

laying out formitems in flex

for some reason my code is not having the desired effect of having labels to the left of the textinput fields*(like in nearly all normal scenarios with forms). The labels are appearing above the textinput fields. I would also like to make some of them read only. Any advice much appreciated
Basically what i have is text sitting above the textbox instead of alongside it to the left.
Here is my code:
<mx:VBox width="100%" height="100%">
<mx:HBox>
<mx:FormItem textAlign="left" <mx:Label text="TEST" textAlign="left" fontSize="14" fontWeight="bold"/>
<mx:TextInput enabled="true" textAlign="right"/> </mx:FormItem>
Use the label field of the formitem.
<mx:FormItem textAlign="left" label="TEST" fontSize="14" fontWeight="bold"/>
<mx:TextInput enabled="true" textAlign="right">
</mx:FormItem>
If you want read-only, then you can put editable="true|false" in the TextInput.

How to style label attributes in Flex forms?

I've got FormItems with labels, and I'd like for the label to appear on top of the textinput within it rather than to its left, but I don't know where I need to go or what CSS I need to set this.
<mx:Form id="myform" defaultButton="{BtnSave}">
<mx:FormItem label="MyData" required="true">
<s:TextInput id="dataTextInput" text="{data}"/>
</mx:FormItem>
</mx:Form>
I don't think this is possible w/ MX Forms. I guess in theory you could do something like this:
<mx:Form id="myform" defaultButton="{BtnSave}">
<mx:FormItem required="true" direction="vertical">
<s:Label text="MyData" />
<s:TextInput id="dataTextInput" text="{data}"/>
</mx:FormItem>
</mx:Form>
But, If you're going to do that then why bother using the form in the first place?

MXML: combobox width is larger than parent width

I have a combobox with a width set to 100%. However, when one of its elements is larger, the combobox grows larger aswell, creating scrollbars and other uglyness in my app!
How do I keep the combobox contained within its parent?
NB it's OK if the list that drops down is larger as long as the closed combobox stays smaller.
Sample:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
<!-- I'm using a Canvas instead of a VBox because the VBox spaces the elements too far appart -->
<mx:HBox id="tagsHBox" width="{formsHBox.width - 16}" x="8" y="8">
<!-- This label should align with the labels in the left form -->
<mx:Label text="Tags" id="tabLabel" width="{titleTxt.x + 4}" textAlign="right" />
<!-- This textbox should spread accross both forms, that's why it's in a seperate HBox -->
<mx:TextInput height="20" width="100%" />
</mx:HBox>
<mx:HBox id="formsHBox" x="8" y="{8 + tagsHBox.height}" width="{this.width-16}">
<mx:Form id="leftForm" width="50%">
<!-- Personal details -->
<mx:FormHeading label="Personal Details" width="100%" />
<mx:FormItem label="First name" width="100%">
<mx:TextInput text="{person.firstName}" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Last name" width="100%">
<mx:TextInput text="{person.lastName}" width="100%"/>
</mx:FormItem>
<!-- And 15 more formItems :) -->
</mx:Form>
<mx:Form id="rightForm" width="50%">
<!-- Address -->
<mx:FormHeading label="Address" width="100%" />
<mx:FormItem label="Street" width="100%">
<mx:TextInput text="{person.address.street}" width="100%"/>
</mx:FormItem>
<mx:FormItem label="City" width="100%">
<mx:TextInput text="{person.address.city}" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Country" width="100%">
<!-- This combobox right here is the troublemaker. There's a
country named 'South Georgia and the South Sandwich
Islands' consising of a few small islands in the southern
pacific and a name which is too long for my innocent
unsuspecting combobox -->
<form:ComboBox id="countryCombo" height="20" width="100%"
dataProvider="{model.systemDataModel.countries}" />
</mx:FormItem>
<!-- And 15 more formItems :) -->
</mx:Form>
</mx:HBox>
</mx:Canvas>
You might be able to use minWidth instead. Set it to zero or some other low value. I know it works with containers like HBox and VBox to make them stop growing larger than their parent container, so it might work with ComboBox too. Basically, what happens is that minWidth="0" overrides the measuredMinWidth, which is a value that the parent container normally respects as the minimum possible size, and it may be bigger than the container's own bounds.
I had the same issue and I solve it easily. I had a country comboBox and a state comboBox components and dynamically filled with country names and the other with states related... I had two forms inside an HBox to show "two columns like" forms and inside the right side form there was a formItem and inside the formItem the comboBox. The problem was when I gave the comboBox its dataProvider then scrollBars appeared and it was very disgusting...
The solution: I show you just the right form because it was the problem (autoLayout="false" in Form and minWidth="0" in ComboBox definition)
<mx:Form autoLayout="false" verticalGap="12">
<mx:FormItem label="Country" required="false" width="100%" direction="vertical">
<mx:ComboBox id="countryComboBox" minWidth="0" width="100%" labelField="#name"/>
</mx:FormItem>
<mx:FormItem label="State" required="false" width="100%" direction="vertical">
<mx:ComboBox id="stateComboBox" minWidth="0" width="100%" labelField="#name"/>
</mx:FormItem>
</mx:Form>
You can use the maxWidth attribute with an absolute size (in pixels) however part of the combobox items ( which are larger then the combobox ) will be cropped .
from adobe :
combobox default size :
Wide enough to accommodate the longest entry in the drop-down list in the display area of the main control, plus the drop-down button. When the drop-down list is not visible, the default height is based on the label text size.

How do I set a focus property on a TextInput that is built using AddChild?

I have the following MXML:
<mx:State name="myState">
<mx:AddChild relativeTo="{myhbox}" position="after">
<mx:Box verticalAlign="middle" horizontalAlign="center" width="100%" height="100%">
<mx:Form id="myForm" width="479" verticalScrollPolicy="off" horizontalScrollPolicy="off">
<mx:FormItem label="My Label:" fontWeight="bold" id="myLabel" direction="vertical">
<mx:TextInput id="myTextInput" width="282" />
<mx:HBox>
<mx:Button label="Go" click="go();" id="goButton" />
</mx:HBox>
</mx:FormItem>
</mx:Form>
</mx:Box>
</mx:AddChild>
</mx:State>
How do I set focus on the TextInput field using <mx:SetProperty/>? I've tried the following, but it only results in the field being highlighted -- the cursor does not appear in the TextInput:
<mx:SetProperty target="{stage}" name="focus" value="{myTextInput}"/>
Long story short, I want the cursor to appear in the field.
UPDATE: I figured it out. See comments for solution.
I try to avoid using the AddChild state tag. It's usually better to put all that in a component, and use SetProperty to set visible and includeInLayout when you want it to display.
You can always override visible in your custom component to set the focus to the field. Or create a custom setter than does the same thing
public function set show(value:Boolean):void
{
visible = true;
includeInLayout = true;
if (value)
myFunctionThatSetsTheFocus();
}
Add a "creationComplete" to the TextInput and had it call a method that setFocus on the TextInput

Resources