dropdownlist event - actionscript-3

I have been searching all around for a guide on event handling in flash builder 4.5. I have a dropdownlist that I'd like to activate preferably a action script function. similar to asp.net/js.
cheers!

right out of as3 docs with some comments...
import fl.controls.ComboBox;
import fl.controls.Label;
var myComboBox:ComboBox = new ComboBox();
myComboBox.prompt = "Please select an item...";
myComboBox.addItem({label:"Item 1"});
myComboBox.addItem({label:"Item 2"});
myComboBox.addItem({label:"Item 3"});
myComboBox.addItem({label:"Item 4"});
myComboBox.width = 150;
myComboBox.move(10, 10);
myComboBox.addEventListener(Event.CHANGE, changeHandler); // <- ASSIGN EVENT LISTENER
addChild(myComboBox);
var myLabel:Label = new Label();
myLabel.autoSize = TextFieldAutoSize.LEFT;
myLabel.text = "selectedIndex:" + myComboBox.selectedIndex;
myLabel.move(myComboBox.x + myComboBox.width + 10, myComboBox.y);
addChild(myLabel);
function changeHandler(event:Event):void { // <- ASSIGN FUNCTION
myLabel.text = "selectedIndex:" + myComboBox.selectedIndex;
}
Also from the docs, these are the events...
change
Dispatched when the user changes the selection in the ComboBox component or, if the ComboBox component is editable, each time the user enters a keystroke in the text field. ComboBox
close
Dispatched when the drop-down list is dismissed for any reason. ComboBox
enter
Dispatched if the editable property is set to true and the user presses the Enter key while typing in the editable text field. ComboBox
itemRollOut
Defines the value of the type property of an itemRollOut event object. ComboBox
itemRollOver
Defines the value of the type property of an itemRollOver event object. ComboBox
open
Dispatched when the user clicks the drop-down button to display the drop-down list. ComboBox
scroll
Dispatched when the user scrolls the drop-down list of the ComboBox component. ComboBox

Related

jpopupmenu where to add listener

I have a jTextField with a button along side to call a popupmenu. The popupmenu contains a list of standard text for the jTextField. This list is held in a list array of variable length, since it can be added too elsewhere in the program.
I'm using the following to generate the popupmenu.
for(i=0;i<=menuArray1.size()-1;i++){
JMenuItem item = new JMenuItem((String) menujlArray1.get(i));
jPopupMenu1.add(item);
}
My question is how do I include the listener that captures which item on the popup menu is selected so I can then put the value into the jTextField.
You need to add a ItemListener interface. If your JMenuItem implements ItemSelectable interface. You can change your code to be like below:
ItemListener il =
e -> {JMenuItem source = (JMenuItem)(e.getSource());
String s = "Item event detected on '" + source.getText() +",New state: "
+ ((e.getStateChange() == ItemEvent.SELECTED) ?
"selected":"unselected");
jTextField.setText(s);
};
for(i=0;i<=menuArray1.size()-1;i++){
JMenuItem item = new JMenuItem((String) menujlArray1.get(i));
jPopupMenu1.add(item);
item.addItemListener(il);
}
More examples here and here

Remove focus from input on click outside

I'm using Flex 4.6 with spark components. I have form with DropDownList, and I want to achieve next behaviour:
user clicks on text input in DropDownList -> DropDownList get focus
user clicks outside (on background Rect, for example) -> DropDownList lose focus
Suddenly, second part doesn't work from box. DropDownList still in focus when user clicks outside the text input. How to implement required behaviour?
Example:
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
]]></fx:Script>
<s:TextInput />
</s:Application>
If you click on TextInput it gets focus. And you cannot remove this focus by clicking outside TextInput.
I found solution at the official forum. There is a post with same problem.
And working solution in the end:
textInput.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, textInputMouseFocusChange, false, 0, true);
// basically the above Event will dispatch when ever the user clicked outside of textinput when the current focus in in textInput.
private function textInputMouseFocusChange(event:FocusEvent):void {
// basically u dont need this first line if u are not calling any function on textInput focusout.
textInput.dispatchEvent(new FocusEvent(FocusEvent.FOCUS_OUT));
// the remaing peice of code will remove the current focus from textInput to the area where the user had clicked.
// this piece of code i have taken from one of the comments in the blog : http://blog.flexexamples.com/2008/09/23/setting-focus-in-flex-using-the-focus-manager/
var focusPoint:Point = new Point();
focusPoint.x = stage.mouseX;
focusPoint.y = stage.mouseY;
var i:int = (stage.getObjectsUnderPoint(focusPoint).length);
stage.focus=InteractiveObject(stage.getObjectsUnderPoint(focusPoint)[i-1].parent);
}
First of all, I think that you are speaking about the ComboBox spark component and not the DropDownList because you have mentioned the TextInput of the component which is a Label control in the case of a DropDownList.
Then, to remove the focus from the ComboBox component, you can set the stage.focus to null when the change event of your component is fired :
<s:ComboBox change="on_Change(event)" />
And
protected function on_Change(event:Event):void
{
stage.focus = null;
}
Hope that can help.

AS3 Flash change combobox value cause keydown events are disabled

I'm using Flash Pro CS6.
I created a fla project and put in a ComboBox.
Then i place following code in action:
import flash.events.KeyboardEvent;
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
this.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
function onKeyDown(e:KeyboardEvent)
{
trace("Keydown :" + e.charCode);
}
function onKeyUp(e:KeyboardEvent)
{
trace("Keyup :" + e.charCode);
}
both event work fine at the beginning but when i change the ComboBox value, the event KEY_DOWN no longer work.
here is my sample project:
Untitled-1.fla
As Baris said, you should use stage.focus = null after selecting combobox element :
cb.addEventListener(Event.CHANGE, cb_on_change)
function cb_on_change(e:Event):void {
stage.focus = null
}
Click anywhere on the stage dont solve problem if there is no element on the stage that will receive the focus.
When you click on the combobox it gets focus which prevents keyboard events from firing on the stage.
Do they start working again if you click anywhere on the stage. Alternatively you can clear the focus with stage.focus = null.

how to check which button is clicked in action script 3.0

I am making game in action script i want that when user selest any region etc then the value of that region must be selected i have 7 regions. how to identify which is the clicked button or selected region
Your question not very clear, but you can add a mouse listener to check if a button is clicked. You can use event.target to detect which button you actually clicked on.
this.mcButton1.addEventListener(MouseEvent.CLICK, handleButtonClick);
this.mcButton2.addEventListener(MouseEvent.CLICK, handleButtonClick);
this.mcButton3.addEventListener(MouseEvent.CLICK, handleButtonClick);
function handleButtonClick(event:MouseEvent):void
{
var button:DisplayObject = DisplayObject(event.target);
trace('I am clicked: ' + button);
}

How to get ContextMenuItem x and y coordinates

i build a contextmenu i want when user click any item then i get the x and y coordinate of the contextmenuitem............actully i want to dispaly a textbox infornt of contextmenuitem when user click on the item........ or any other solution that i will show inputtext control as submenuitem in contextmenuitem
The only way I can think of doing what you are asking for is:
disallow the right-click mouse in the
html container with javascript
capture right-click events and
forward them to flash via
ExternalInterface
In the method
triggered from ExternalInterface,
do/show what you want.
There are some open source solutions:
custom-context-menu
Article at ADM blog
Add an event listener to each menu item. In the listener function, the event's target object is the object you clicked on - all you need to do is cast it to DisplayObject, and you can access the x and y coordinates:
contextmenuItem.addEventListener (MouseEvent.CLICK, onItemClick);
function onItemClick (ev:MouseEvent) : void {
var item:DisplayObject = ev.target as DisplayObject;
// use item.x and item.y to get the object's position.
}