Flex VerticalLayout HorizontalLayout element order based on state - apache-flex

in my mxml part I have
<s:layout.landscape>
<s:HorizontalLayout />
</s:layout.landscape>
<s:layout.portrait>
<s:VerticalLayout />
</s:layout.portrait>
after these tags i have couple of components, for example
<s:Button label="button 1"/>
<s:Button label="button 2"/>
what I would like to do is change the order of these components once a portrait or landscape state is entered. For example in portrait I have vertical layout button 1 followed by button 2
and in landscape I have horizontal layout where button2 is followed by button 1.

You can use this.swapElements(button1,button2);
Example: -
<?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/mx"
minWidth="955" minHeight="600" currentState="landscape">
<s:states>
<s:State name="landscape"/>
<s:State name="portrait"/>
</s:states>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
protected function dropdownlist1_changeHandler(event:IndexChangeEvent):void
{
// TODO Auto-generated method stub
if(event.target.selectedIndex == 0)
{
this.currentState = event.target.dataProvider[0];
this.swapElements(button1,button2);
}
else
{
this.currentState = event.target.dataProvider[1];
this.swapElements(button2,button1);
}
}
]]>
</fx:Script>
<s:layout.landscape>
<s:HorizontalLayout />
</s:layout.landscape>
<s:layout.portrait>
<s:VerticalLayout />
</s:layout.portrait>
<s:DropDownList selectedIndex="0" change="dropdownlist1_changeHandler(event)">
<s:ArrayCollection>
<fx:String>landscape</fx:String>
<fx:String>portrait</fx:String>
</s:ArrayCollection>
</s:DropDownList>
<s:Button id="button1" label="button 1"/>
<s:Button id="button2" label="button 2"/>
</s:Application>
Hope this may help...

Related

error when using custom class in itemrenderer

I created a subclass of Event Class. I used the subclass in an itemrenderer, no error observed. But once I declared the itemrenderer to the List in the Main application, errors appears in the itemrenderer claiming "Type was not found or was not a compile-time constant: CustomDeleteEvent" and "Incorrect number of arguments: Expected no more than 1"
Please give me some advice. Thanks in advance.
In subclass :
package widgets.GetMap
{
import flash.events.Event;
public class CustomDeleteEvent extends Event
{
public static const DELETE_ITEM:String = "DELETE_ITEM";
public var deletedItem:String;
public function CustomDeleteEvent(type:String, deletedItem:String)
{
super(type);
this.deletedItem = deletedItem;
}
}
}
In ItemRenderer :
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer name="CustomItemRen"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true"
minHeight="24">
<s:states>
<s:State name="normal" />
<s:State name="hovered" />
<s:State name="selected" />
</s:states>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
import spark.components.List;
protected function deleteHandler():void
{
var deleteItem:String = itemIndex.toString();
var tryevent:CustomDeleteEvent;
owner.dispatchEvent(tryevent,deleteItem);
Object(owner).dataProvider.removeItemAt(itemIndex);
}
]]>
</fx:Script>
<s:HGroup width="100%" height="100%"
verticalAlign="middle"
paddingLeft="2" paddingRight="2"
paddingTop="2" paddingBottom="2">
<s:Label id="lbl" text="{data.toString()}" width="100%" color="#30FF00"/>
<s:Button id="btn" includeIn="hovered,selected" y="-16" width="35" height="22" label="X"
accentColor="#FFFFFF" color="#FF0000" fontFamily="Verdana" fontSize="12"
fontWeight="bold" mouseDown="deleteHandler();" toolTip="Delete item"/>
</s:HGroup>
</s:ItemRenderer>
In the main application:
<s:Application name="Spark_List_itemRenderer_hovered_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Style source="test.css"/>
<s:List id="lst"
itemRenderer="CustomItemRen"
width="300"
horizontalCenter="0" verticalCenter="0">
<s:layout>
<s:VerticalLayout gap="0"
horizontalAlign="justify"
requestedRowCount="8" />
</s:layout>
<s:dataProvider>
<s:ArrayList>
<fx:Object label="Application" />
<fx:Object label="Label" />
<fx:Object label="List" />
</s:ArrayList>
</s:dataProvider>
</s:List>
</s:Application>
In this line, you didn't initialize the event object ("tryEvent" like new CustomDeleteEvent())..
and also in your "CustomDeleteEvent"..there is "type" parameter, which is not in "tryEvent" object...
var tryevent:CustomDeleteEvent;
owner.dispatchEvent(tryevent,deleteItem);
Use below code for dispatch the event....
owner.dispatchEvent(new CustomDeleteEvent(CustomDeleteEvent.DELETE_ITEM, deleteItem));
hope this will help you....

Adobe flex from a list componet to radio buttons

I have tried to do this many ways but for the life of me cannot think how to. Basically I have a list. When an item on the list is selected the radio button labels change. However, I want a label and text area to appear saying once the user has clicked on the radio button if it is right or not.
Code so far: -
<s:VGroup x="103" y="130" width="123" height="125">
<s:RadioButton id="RadioButton1" label="{data.QuestionsRadioButton1}" groupName="QuestionsTestRadioButtons" click="RadioButton1_clickHandler(event)"/>
<s:RadioButton label="{data.QuestionsRadioButton2}" groupName="QuestionsTestRadioButtons" click="radiobutton1_clickHandler(event)" />
<s:RadioButton id="RadioButton3" label="{data.QuestionsRadioButton3}" groupName="QuestionsTestRadioButtons" click="radiobutton2_clickHandler(event)"/>
</s:VGroup>
I will not post all the code as we will be here forever. However, is there a way maybe an if function? To say if the radio button clicked actually is the right answer or not?
Any suggestions would be helpful
Thank You
My attempt to draft a complete example:
Questionaire.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:local="*">
<fx:Declarations>
<s:ArrayCollection id="questions">
<local:Question text="It is raining …">
<s:ArrayCollection>
<local:Answer text="Men"/>
<local:Answer text="Cats and dogs" correct="true"/>
<local:Answer text="Candy"/>
</s:ArrayCollection>
</local:Question>
<local:Question text="The sky is …">
<s:ArrayCollection>
<local:Answer text="Blue" correct="true"/>
<local:Answer text="Orange" correct="true"/>
<local:Answer text="Grey" correct="true"/>
<local:Answer text="Green"/>
</s:ArrayCollection>
</local:Question>
</s:ArrayCollection>
</fx:Declarations>
<s:DataGroup dataProvider="{questions}" itemRenderer="QuestionRenderer">
<s:layout>
<s:VerticalLayout gap="24"/>
</s:layout>
</s:DataGroup>
</s:Application>
QuestionRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:DataRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
[Bindable("dataChange")]
public function get question():Question {
return data as Question;
}
]]>
</fx:Script>
<fx:Declarations>
<s:RadioButtonGroup id="answerGroup"/>
</fx:Declarations>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Label fontWeight="bold" text="{question.text}"/>
<s:DataGroup dataProvider="{question.answers}">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:itemRenderer>
<fx:Component>
<s:DataRenderer>
<fx:Script>
<![CDATA[
import spark.components.RadioButtonGroup;
public function get answerGroup():RadioButtonGroup {
return outerDocument.answerGroup;
}
]]>
</fx:Script>
<s:RadioButton groupName="answerGroup" label="{data.text}" value="{data}"/>
</s:DataRenderer>
</fx:Component>
</s:itemRenderer>
</s:DataGroup>
<s:Label visible="{answerGroup.selectedValue}" text="This is {answerGroup.selectedValue.correct ? 'correct' : 'incorrect'}."/>
</s:DataRenderer>
Question.as
package {
import mx.collections.ArrayCollection;
[Bindable]
[DefaultProperty("answers")]
public class Question {
public var text:String;
public var answers:ArrayCollection;
}
}
Answer.as
package {
[Bindable]
public class Answer {
public var text:String;
public var correct:Boolean = false;
}
}

There are many buttons, when click on one of them, how to change its label?

I'm writing a very simple flex application with mxml. I have many buttons, when I click one of them, I hope its value changes to World.
My code is:
<?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/mx" minWidth="955" minHeight="600">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function hello():void {
this.label = "World!";
}
]]>
</fx:Script>
<mx:HBox>
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
</mx:HBox>
</s:Application>
Which is incorrect since this.label = "World!" can't be compiled that this.label is not found.
How to let the this reference to the button I clicked, or how to implement this ?
Try below code this may help you: -
<?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/mx" minWidth="955" minHeight="600">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function hello(event:MouseEvent):void {
event.currentTarget.label = "World!";
}
]]>
</fx:Script>
<mx:HBox>
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
</mx:HBox>
</s:Application>

Why do children of my popup panel appear outside of the panel

I am using the flex 4 popup manager to popup a panel, but the children of the panel are not staying inside the panel. But when i close the popup the children are being removed.
Like this: (sorry i can't post pics)
----------
l________l
l l
..... please enterl
l--------1
Anybody know why? Here's my code:
var forgotPopup:Panel = PopUpManager.createPopUp(this, forgottenForm, true) as Panel;
PopUpManager.centerPopUp(forgotPopup);
And here's what i'm popping up:
<?xml version="1.0" encoding="utf-8"?>
<s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="218" height="168" skinClass="PanelSkin" title="Reset Details">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
// Handle the close button and Cancel button.
private function handleCloseEvent():void {
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<mx:Form horizontalCenter="0" verticalCenter="0">
<mx:FormHeading label="Please enter your e-mail address and your login details will be e-mailed to you"/>
<mx:FormItem label="E-mail">
<s:TextInput id="userInput" x="78" y="49"/>
</mx:FormItem>
<mx:FormItem direction="horizontal">
<s:Button id="okButton" label="Submit" skinClass="ButtonSkin" />
<s:Button id="cancelButton" label="Cancel" skinClass="ButtonSkin"/>
</mx:FormItem>
</mx:Form>
</s:Panel>
Any help on this would be brilliant, thanks.
Try to use the following:
<?xml version="1.0" encoding="utf-8"?>
<s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="218" height="168" skinClass="PanelSkin" title="Reset Details">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
// Handle the close button and Cancel button.
private function handleCloseEvent():void {
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<mx:Form left="10" right="10" top="10" bottom="10">
<mx:FormHeading label="Please enter your e-mail address and your login details will be e-mailed to you"/>
<mx:FormItem label="E-mail">
<s:TextInput id="userInput" x="78" y="49"/>
</mx:FormItem>
<mx:FormItem direction="horizontal">
<s:Button id="okButton" label="Submit" skinClass="ButtonSkin" />
<s:Button id="cancelButton" label="Cancel" skinClass="ButtonSkin"/>
</mx:FormItem>
</mx:Form>
</s:Panel>
If you need to have header word wrapped you should reject standard FormHeading and replace it with Label:
<?xml version="1.0" encoding="utf-8"?>
<s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="218" height="168" skinClass="PanelSkin" title="Reset Details">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
// Handle the close button and Cancel button.
private function handleCloseEvent():void {
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<mx:Form left="10" right="10" top="10" bottom="10">
<s:Label width="100%" text="Please enter your e-mail address and your login details will be e-mailed to you" fontWeight="bold" />
<mx:FormItem label="E-mail">
<s:TextInput id="userInput" x="78" y="49"/>
</mx:FormItem>
<mx:FormItem direction="horizontal">
<s:Button id="okButton" label="Submit" skinClass="ButtonSkin" />
<s:Button id="cancelButton" label="Cancel" skinClass="ButtonSkin"/>
</mx:FormItem>
</mx:Form>
</s:Panel>
But the best way is to switch to Spark Form which is available since Flex 4.5.
Please Avoid X and Y position
this might also cause the Objects Positions.

spark list item renderer

How do I wordwrap in a spark list w/ an itemrenderer? This posting at http://blog.flexexamples.com/2009/10/27/setting-word-wrapping-on-a-spark-list-control-in-flex-4/ works 100% but when I try to set a separate item renderer, I can't get the word wrapping...instead, I get an ugly horizontal scroll bar. HEre is my 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" width="100%"
height="100%"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:List id="lst"
useVirtualLayout="false"
width="200"
horizontalCenter="0" verticalCenter="0"
itemRenderer="testRenderer"
>
<s:dataProvider>
<s:ArrayList>
<fx:Object />
</s:ArrayList>
</s:dataProvider>
</s:List>
</s:Application>
here is my testRenderer ------------>
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer name="testRenderer"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true"
creationComplete="makeLabel(event);">
<s:states>
<s:State name="normal" />
<s:State name="hovered" />
<s:State name="selected" />
</s:states>
<s:layout>
<s:VerticalLayout gap="0" horizontalAlign="justify" />
</s:layout>
<fx:Script>
<![CDATA[
protected function makeLabel(evt:Event):void {
test.text = "this is just a really long line of text that I want to wrap and just look normal";
}
]]>
</fx:Script>
<s:HGroup>
<s:Label id="test" color="black" />
</s:HGroup>
</s:ItemRenderer>
try giving your Label (id="test") a width

Resources