Hide a Button in a ButtonBar - actionscript-3

I was wondering if there is any way you hide a particular button in a ButtonBar. According to this answer (and the link provided in the second answer) Disable individual buttons in a buttonbar I need to use the getChildAt method of the ButtonBar, but when I do that I get the custom skin object and not the Button object. I was wondering how I could get access to the Button object.
Thanks!

Under the assumption that all buttons in your button bar will be rendered at the same time and you won't need scrollbars...
With a Spark ButtonBar, you can access the skin part directly to get access to a button. Conceptually something like this:
var button : Button = mySparkButtonBarInstance.dataGroup.getElementAt(SomeIndex);
button.visible = false; // or true
button.includeInLayout = false; // or true
This won't work if your ButtonBar can make use of Virtual Layouts and requires scrolling.
Edit: Here is working code demonstrating this technique:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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:Script>
<![CDATA[
import mx.core.IVisualElement;
protected function button1_clickHandler(event:MouseEvent):void
{
trace(buttonBar.dataGroup.getElementAt(0));
var button :IVisualElement = buttonBar.dataGroup.getElementAt(0);
button.visible = false; // or true
button.includeInLayout = false; // or true }
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:layout>
<s:VerticalLayout paddingLeft="20" paddingTop="20"/>
</s:layout>
<s:ButtonBar id="buttonBar">
<mx:ArrayCollection>
<fx:String>Flash</fx:String>
<fx:String>Director</fx:String>
<fx:String>Dreamweaver</fx:String>
<fx:String>ColdFusion</fx:String>
</mx:ArrayCollection>
</s:ButtonBar>
<s:Button label="Remove" click="button1_clickHandler(event)" />
</s:WindowedApplication>

Related

Dynamically create button in Flex

I have very basic question about Flex SDK, but I didn't find any resources for my problem and honestly don't know if it even possible to do what I want. So here is my question:
I created Flex project with Adobe Flex Builder 4.6. Then I placed the button (let's say it's id is btn1) in main MXML file. I want to create second button dynamically right from the script part of main MXML file. Specifically I want to create it from button click handler of btn1.
Here is my MXML code (it is the only file in project ):
<?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:Script>
<![CDATA[
protected function btn1_clickHandler(event:MouseEvent):void
{
var btn2:Button = new Button();
btn2.label = "Hello";
btn2.x = 50;
btn2.y = 50;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Non visual elements -->
</fx:Declarations>
<s:Button id="btn1"
x="10" y="10"
label="Кнопка"
click="btn1_clickHandler(event)"/>
</s:Application>
But when I click btn1 - nothing happens. It is possible that I don't understand something in flex programming paradigm - please point it out for me.
You have to add the button to the view using addElement().
var btn2:Button = new Button();
btn2.label = "Hello";
btn2.x = 50;
btn2.y = 50;
addElement(btn2);

Apply effect on window in Flex, Air, AS3

I'm looking to animate a popup window using the effect.
The problem is that the effect is applied to the content of the popup window, not the popup window itself (including the title bar, minimize, maximize buttons etc)
Have a look at the result here..
My code is really simple and logically, should work if it is possible to animate a window.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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:Script>
<![CDATA[
import comps.MyWin;
[Bindable]
private var _win:MyWin;
protected function openPopup():void
{
_win = new MyWin();
_win.width = 300;
_win.height = 300;
_win.open();
}
protected function animatepopup():void
{
MyEffect.play();
}
]]>
</fx:Script>
<fx:Declarations>
<s:Move id="MyEffect" xFrom="{_win.x}" xTo="{_win.x + 150}" target="{_win}"/>
</fx:Declarations>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button label="Open" click="openPopup()"/>
<s:Button label="Animate" click="animatepopup()"/>
</s:WindowedApplication>
I think you need to target the object's NativeWindow instance in order to move and resize the object.
Thus, replace _win.myProperty with _win.stage.nativeWindow.myProperty:
<s:Move id="MyEffect" xFrom="{_win.stage.nativeWindow.x}" xTo="{_win.stage.nativeWindow.x + 150}" target="{_win.stage.nativeWindow}"/>
Then, the animation will affect the NativeWindow and not the internals of the window.

Exception of null object reference when click on Text area in popup in Flash Builder 4.6

Here is my code.
<?xml version="1.0" encoding="utf-8"?>
<fx:Script>
<![CDATA[
import comps.sampleTextArea;
import mx.managers.PopUpManager;
protected function button1_clickHandler(event:MouseEvent):void
{
var pop:sampleTextArea = new sampleTextArea();
PopUpManager.createPopUp(this, sampleTextArea, false);
PopUpManager.centerPopUp(pop);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button click="button1_clickHandler(event)" label="open popup"/>
and here is code of popup
<?xml version="1.0" encoding="utf-8"?>
<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;
protected function button1_clickHandler(event:MouseEvent):void
{
ta.text = '';
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="top" />
</s:layout>
<s:TextArea id="ta" width="100%" height="90%">
</s:TextArea>
<s:Button label="Submit" click="button1_clickHandler(event)" />
when i click on text area following error through by the application.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at spark.components::Scroller/focusInHandler()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\Scroller.as:2139]
at flash.display::Stage/set focus()
at flashx.textLayout.container::ContainerController/http://ns.adobe.com/textLayout/internal/2008::setFocus()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\container\ContainerController.as:2265]
at flashx.textLayout.container::ContainerController/mouseDownHandler()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\container\ContainerController.as:2067]
at flashx.textLayout.container::TextContainerManager/mouseDownHandler()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\container\TextContainerManager.as:1939]
at spark.components.supportClasses::RichEditableTextContainerManager/mouseDownHandler()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\supportClasses\RichEditableTextContainerManager.as:666]
at flashx.textLayout.container::ContainerController/http://ns.adobe.com/textLayout/internal/2008::requiredMouseDownHandler()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\container\ContainerController.as:2088]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:13152]
at mx.managers::SystemManager/mouseEventHandler()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:2918]
How i handle this issue
try, because this refer to stage no the clicked element:
PopUpManager.removePopUp(event.target);
You may not be able to popup a focusable component inside a
non-IFocusManagerContainer. If your Group container implements IFocusManagerContainer class, you may use PopUpManager.
<s:Group implements="mx.managers.IFocusManagerContainer"/>
I ran into the same issue, and the root cause was that PopUpManager/PopUpAnchor would not properly set the focusManager if the component being popped up does not implement the IFocusManagerContainer interface. After implementing such interface, the problem goes away.
You may read this blog post that inspired the solution.
After doing many experiments/ways i conclude that problem is due to the popup component parent container.
I use Group/VGroup/HGroup of spark then this issue remains but if i change the parent container with spark Panel/SkinnableContainer then issue solved.
Try yourself and enjoy.

A link inside TextFlow: rollOver/rollOut bubbles, shouldn't bubble, and cannot be avoided

I'm having a weird behavior. I have an HTML link inside a TextFlow. When I set its linkHoverFormat, it starts sending rollOver/rollOut events up the chain. These events say the don't bubble but somehow they do. And I can't seem to stop them from doing so.
Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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:Declarations>
<s:TextLayoutFormat id="linkNormal" textDecoration="none" color="0x556677"/>
<s:TextLayoutFormat id="linkHover" textDecoration="underline" color="0x0000FF"/>
<s:TextFlow id="textFlow1"
linkActiveFormat="{linkHover}"
linkHoverFormat="{linkHover}"
linkNormalFormat="{linkNormal}">
<s:p> <s:a href="http://projects.nunogodinho.com">hover</s:a> </s:p>
</s:TextFlow>
</fx:Declarations>
<fx:Script>
<![CDATA[
// This pair of handlers is triggered by the panel
protected function rollOverHandler(e:MouseEvent):void {
panel.title = "over";
}
protected function rollOutHandler(e:MouseEvent):void {
panel.title = "out";
}
]]>
</fx:Script>
<s:Panel left="10" top="10" id="panel" mouseOver="rollOverHandler(event)" mouseOut="rollOutHandler(event)">
<s:RichEditableText id="ret1"
top="10"
editable="false"
textFlow="{textFlow1}" />
</s:Panel>
</s:WindowedApplication>
If you run this app and move the mouse over the link several times without moving out of the panel you'll see that it still changes the text.
So I decided to try to stop this strange event from bubbling:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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:Declarations>
<s:TextLayoutFormat id="linkNormal" textDecoration="none" color="0x556677"/>
<s:TextLayoutFormat id="linkHover" textDecoration="underline" color="0x0000FF"/>
<s:TextFlow id="textFlow2"
linkActiveFormat="{linkHover}"
linkHoverFormat="{linkHover}"
linkNormalFormat="{linkNormal}">
<s:p> <s:a rollOver="linkelement2_rollOverHandler(event)" rollOut="linkelement2_rollOutHandler(event)" href="http://projects.nunogodinho.com">hover</s:a> </s:p>
</s:TextFlow>
</fx:Declarations>
<fx:Script>
<![CDATA[
import flashx.textLayout.events.FlowElementMouseEvent;
// This pair of handlers is triggered by the panel
protected function rollOverHandler(e:MouseEvent):void {
panel.title = "over";
}
protected function rollOutHandler(e:MouseEvent):void {
panel.title = "out";
}
// This pair of handlers is triggered by the <A> element inside textFlow2
// and I hoped it would stop the event from bubbling up to the panel
protected function linkelement2_rollOverHandler(e:FlowElementMouseEvent):void {
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation()
}
protected function linkelement2_rollOutHandler(e:FlowElementMouseEvent):void {
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation()
}
// I also tried to intercept the event in the RichEditableText but got the same weird
// behavior: the event triggers the panel's rollOver/rollOut intermittently
]]>
</fx:Script>
<s:Panel left="10" top="10" id="panel" mouseOver="rollOverHandler(event)" mouseOut="rollOutHandler(event)">
<s:RichEditableText top="10" left="55"
editable="false"
textFlow="{textFlow2}" />
</s:Panel>
</s:WindowedApplication>
I tried all combinations of preventDefault, stopImmediatePropagation and stopPropagation. It does change the event's behavior but it also ruins the hovering effect on the link.
Then I tried to intercept the event at the RichEditableText but I got the same kind of results.
Once I had a similar problem but I learned that it was because I was using mouseOver/mouseOut events instead of rollOver/rollOut. Since I switched to rollOver/rollOut it worked fine. Until now.
And now I'm clueless.
Please help.
Thanks.

Highlight the selected button in controlbar

I have 3 buttons(say b1,b2,b3) in my app which are under the controlbar
on clicking those buttons it will open new view(components)
suppose if button b1 is clicked it should highlight the button b1(bg color change).
how to go about this sorry if it is noob question as am new to this
Thanks
I think you should use ToggleButton here is example
also check Button Bar to group buttons
EDITED by you question i think you need to switch views on Button click
in that case you may see Tab Navigator
hopes thst helps
Set toggle property for buttons to true as in documentation and then manage selected property for buttons (set it to true for active button).
Try this example,since you are looking for a solution without any button bars
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
protected function button1_clickHandler(event:MouseEvent):void
{
for each(var child:UIComponent in hbox.getChildren())
{
if(child.className == 'Button')
{
Button(child).selected = false;
}
}
event.currentTarget.selected = true;
}
]]>
</mx:Script>
<mx:HBox id="hbox">
<mx:Button label="B1" toggle="true" click="button1_clickHandler(event)"/>
<mx:Button label="B2" toggle="true" click="button1_clickHandler(event)"/>
<mx:Button label="B3" toggle="true" click="button1_clickHandler(event)"/>
</mx:HBox>
</mx:Application>
For controlling the background color of the buttons in selected state,define selectedUpSkin,selectedOverSkin,selectedDownSkin(and selectedDisabledSkin)
P.S:If you are using only buttons in the control bar,you can use Button as the type of child and avoid that if statement