I'm creating a reusable flex tree component. And i would like to stick in the itemclick function. So that when a user clicks anywhere on one of the tree's Branches. the branch expands.
My problem is that I don't know how I can get the listener function to fire.
What I would like to do is create the tree completely in as3. (no mxml).
Normally I set the itemClick on tree in the mxml. but I want to do this in as3.
My component has a lot more functions in it but I have deleted them so that it becomes easier to read.
Can anyone help me out on this one? I Thought if I override the createChilderen function and add the eventlistener in there, that it would work. But no luck.
this is my code;
package
{
import mx.controls.Tree;
import mx.controls.listClasses.IListItemRenderer;
import mx.events.ItemClickEvent;
import mx.events.ListEvent;
public class MyTree extends Tree
{
public function MyTree()
{
super();
}
private function tree_itemClick(evt:ListEvent):void {
var item:Object = Tree(evt.currentTarget).selectedItem;
if (dataDescriptor.isBranch(item)) {
expandItem(item, !isItemOpen(item), true);
}
}
override protected function createChildren():void{
super.createChildren();
addEventListener(ListEvent.ITEM_CLICK, tree_itemClick, true);
}
}
}
package
{
import mx.controls.Tree;
import mx.events.ListEvent;
public class MyTree extends Tree
{
public function MyTree()
{
super();
addEventListener(ListEvent.ITEM_CLICK, itemClickHandler);
}
private function itemClickHandler(event:ListEvent):void
{
trace("Success");
}
}
}
Related
I've got some problem in Flex. Basically I want to navigate to other pages by using navigator.pushview from list through custom item renderer. This is my CustomItemRender.as. Edit:
package renderer
{
import flash.events.MouseEvent;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.events.ItemClickEvent;
import spark.components.LabelItemRenderer;
import spark.components.NavigatorContent;
import spark.components.ViewNavigator;
public class CustomItemRender extends LabelItemRenderer
{
protected var var_A:Image;
[Bindable]
public var navigator:ViewNavigator = new ViewNavigator();
public function PrgListItemRenderer()
{
super();
}
override public function set data(value:Object):void
{
super.data = value;
}
override protected function createChildren():void
{
super.createChildren();
if(!takeAtt)
{
var_A= new Image();
var_A.source = "data/pics/var_A.png";
var_A.width = 23;
var_A.height = 23;
var_A.buttonMode = true;
var_A.addEventListener(MouseEvent.CLICK, var_AItem);
addChild(var_A);
}
}
override protected function measure():void
{
super.measure();
// measure all the subcomponents here and set measuredWidth, measuredHeight,
// measuredMinWidth, and measuredMinHeight
}
/**
* #private
*
* Override this method to change how the background is drawn for
* item renderer. For performance reasons, do not call
* super.drawBackground() if you do not need to.
*/
override protected function drawBackground(unscaledWidth:Number,
unscaledHeight:Number):void
{
super.drawBackground(unscaledWidth, unscaledHeight);
// do any drawing for the background of the item renderer here
if(selected || showsCaret)
{
graphics.beginFill(0xffffff, 1);
graphics.endFill();
}
}
public function var_AItem(event:MouseEvent):void
{
trace("navigator: "+navigator);
navigator.pushView(nextView); //this is the line that have error #1009
}
}
}
But I got Error #1009. Help me please. Thanks.
I think it's a bad idea to listen to the click event inside the item renderer.
Your basic setup should look something like this:
->ViewNavigatorApplication>
-->SomeCustomView
---> SomeListBasedComponent id="list" itemRenderer="someCustomRenderer"
Fill the list with some data, which will be presented by your itemRenderer.
Now listen to the "IndexCangeEvent" of the list (from your view) and handle the 'click' there
To your view add:
private function init():void
{
list.addEventListener(IndexChangeEvent.CHANGE , onIndexChange );
}
protected function onIndexChange(e:IndexChangeEvent):void
{
// find out which item was selected. You can use the selectedItem property for this
var item:Object = list.selectedItem;
// start the view;
navigator.pushView(MyViewClass , item.someViewData );
}
Your view will hold the reference to the ViewNavigator.
P.S. dont forget to call the init() function onCreationComplete() of your view.
To your view declaration add:
View ... creationComplete="init()" >
I'm very new to Flex 4.5 and I created a class (Project.as) with the following code in it:
package classes
{
public class Project
{
public var projectName:String;
public var description:String;
public var fileLoc:String;
public function Project()
{
// This is the constructor
}
public function SayHello() {
import mx.controls.Alert;
Alert.show('howdy!','Greeting');
}
}
}
In my main.mxml file, I have the following code:
<fx:Script>
<![CDATA[
import classes.Project;
import mx.controls.Alert;
public var aProject:Project = new Project;
aProject.SayHello();
]]>
</fx:Script>
And Flex Builder is saying this:
1120: Access of undefined property aProject.
Why is it telling me this, and how can I fix it? I don't see why it's not working.
Lots of issues here.
First, I have never seen anyone put import statements inside a method. Usually they are put between the package and class definition:
package classes
{
import mx.controls.Alert;
public class Project
{
public var projectName:String;
public var description:String;
public var fileLoc:String;
public function Project()
{
// This is the constructor
}
public function SayHello() {
Alert.show('howdy!','Greeting');
}
}
}
Second; the line of ActionSCript code that you write to call a method on your class instance should be placed inside a method; not "random". Like this:
<fx:Script>
<![CDATA[
import classes.Project;
import mx.controls.Alert;
public var aProject:Project = new Project;
protected function sayHello():void{
aProject.SayHello();
}
]]>
</fx:Script>
Some way you'll want to call that method. A commenter on the original post suggested using creationComplete, which would work. However, you should be cautious about using creationComplete for "constructor-style" code in an MXML Component. preinitialize is better, and the event will fire right after the actual constructor runs. If you need to access any MXML children, have your code in an initialize event handler which runs right after createChildren() runs.
creationComplete handlers execute right after the component finishes initializing; and people often do things in creationComplete that make the component go through it's Lifecycle again, updating the display list.
I want to make a reusable Alert Box Class which will be instantiated on various screens of my Flex Project.
Can some tell me whats next in the code below, because am sort of lost regarding how to set the message and title and how to call the Class in my project?
Any help.
Thanks
package components
{
import mx.controls.Alert;
import mx.core.mx_internal;
public class myAlertBox extends Alert
{
public function AlertBoza()
{
super();
var a:Alert;
}
override public static function show():void{
}
}
}
You do not need to extend Alert since the Alert.show() function is static. But you can set it as follows inserting a constructor for a message string and a class member. With that cou can just call the class with the constructor and show the alertbox.
package components
{
import mx.controls.Alert;
import mx.core.mx_internal;
public class myAlertBox
{
private var message:String;
public function myAlertBox(message:String = "")
{
super();
this.message = message;
}
public function show():void{
Alert.show(message);
}
}
}
In another class you can call:
var box:myAlertBox = new myAlertBox("Error");
myAlertBox.show();
If you just want to show a simple alert box, just use mx.controls.Alert directly as you can specify the title and the message show then:
import mx.controls.Alert;
Alert.show("the message", "the title");
I am trying to do something like this:
package com.clicker{
import flash.display.*;
import flash.events.MouseEvent;
public class Stager extends MovieClip {
public function clicker():void {
stage.addEventListener(MouseEvent.CLICK, do_stage);
}
function do_stage(e:MouseEvent):void {
trace("stage clicked");
}
}
}
But, I get the 1009 error.
When I do this:
import com.clicker.*;
var test:Stager = new Stager();
test.clicker();
addChild(test);
Please help me. Thank you very much in advance, and Happy Holidays.
stage is accessible only when your component is added to the stage. If you want to know it, you can use the ADDED_TO_STAGE event.
So, you can do this :
package com.clicker{
import flash.display.*;
import flash.events.*;
public class Stager extends MovieClip {
public function clicker():void {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(MouseEvent.CLICK, do_stage);
}
function do_stage(e:MouseEvent):void {
trace("stage clicked");
}
}
}
since you call test.clicker(); before it added to the stage test doesn't have a this.stage object yet try :
public class Stager extends MovieClip {
public function clicker():void {
this.addEventListener( Event.ADDED_TO_STAGE , function(ev:Event) {
stage.addEventListener(MouseEvent.CLICK, do_stage);
});
}
function do_stage(e:MouseEvent):void {
trace("stage clicked");
}
}
hope this helps...
I have two classes. The first one (the starting class):
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import tetris.*;
public class TetrisGame extends Sprite
{
private var _gameWell:Well;
public function TetrisGame()
{
_gameWell = new Well();
addChild(_gameWell);
}
}
}
The second:
package tetris
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
public class Well extends Sprite
{
public function Well()
{
super();
addEventListener(KeyboardEvent.KEY_DOWN, onKeyboard);
}
private function onKeyboard(event:KeyboardEvent):void
{
//some code is here
}
}
}
But when I press any buttons on my keyboard, the child class Well doesn't have any reaction. What's the problem?
OK, I get it! =))
I should set focus on the child sprite so it can listen for keyboard events.
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import tetris.*;
public class TetrisGame extends Sprite
{
private var _gameWell:Well;
public function TetrisGame()
{
_gameWell = new Well();
addChild(_gameWell);
stage.focus = _gameWell;
}
}
}
Or as an alternative; add the event listener to the stage, so it doesn't depend on the Well having focus.
package tetris
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Well extends Sprite
{
public function Well():void
{
super();
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboard);
}
private function onKeyboard(event:KeyboardEvent):void
{
//some code is here
}
}
}