changin the default width and height of the dropdownlist - apache-flex

i want a dropdownlist that is wide enough to accommodate the larger item in the dropdown in display area of the main control.(.i.e. dropdownlist with the functionality of MX Combobox)
please guide me on this .

This will adjust the width of the drop down to the width of the data.
I forget where the example is where i got the code from. The height can be set by rowCount I believe.
package valueObjects.comboBox{
import flash.events.Event;
import mx.controls.ComboBox;
import mx.core.ClassFactory;
import mx.core.IFactory;
import mx.events.FlexEvent;
public class ExtendedComboBox extends ComboBox{
private var _ddFactory:IFactory = new ClassFactory(ExtendedList);
public function ExtendedComboBox(){
super();
}
override public function get dropdownFactory():IFactory{
return _ddFactory;
}
override public function set dropdownFactory(factory:IFactory):void{
_ddFactory = factory;
}
public function adjustDropDownWidth(event:Event=null):void{
this.removeEventListener(FlexEvent.VALUE_COMMIT,adjustDropDownWidth);
if (this.dropdown == null){
callLater(adjustDropDownWidth);
}else{
var ddWidth:int = this.dropdown.measureWidthOfItems(-1,this.dataProvider.length);
if (this.dropdown.maxVerticalScrollPosition > 0){
ddWidth += ExtendedList(dropdown).getScrollbarWidth();
}
this.dropdownWidth = Math.max(ddWidth,this.width);
}
}
override protected function collectionChangeHandler(event:Event):void{
super.collectionChangeHandler(event);
this.addEventListener(FlexEvent.VALUE_COMMIT,adjustDropDownWidth);
}
}
}
package valueObjects.comboBox{
import mx.controls.List;
public class ExtendedList extends List{
public function ExtendedList(){
super();
}
public function getScrollbarWidth():int{
var scrollbarWidth:int = 0;
if (this.verticalScrollBar != null){
scrollbarWidth = this.verticalScrollBar.width;
}
return scrollbarWidth;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" mlns:comboBox="valueObjects.comboBox.*" >
<comboBox:ExtendedComboBox labelField="data.totext()" itemRenderer="mx.controls.Label" />
</mx:Canvas>

You should create custom DropDownList (extending original) and override dataProvider setter and measure() method. In dataProvider setter you should invoke invalidateSize() and in measure you should iterate your data provider and find the largest label (you can assign new text to the label and compare their width).

To increase the height of a combo box's drop-down list, use "rowCount" property. This is for Flex 3.6.0

Check out this example from Flex Examples. You just have to create a skin for the DropDownList and set the popUpWidthMatchesAnchorWidth="false" property.

it is achieved by following code :
public override function set dataProvider(value:IList):void
{
var length:Number = 0;
var array:Array = value.toArray();
super.dataProvider = value;
var labelName:String = this.labelField;
var typicalObject:Object =new Object();
for each(var obj:Object in array)
{
var temp:Number = obj[labelName] .length;
if( length < temp )
{
length = temp;
Alert.show(" length "+length.toString());
typicalObject = obj;
}
//length = length < Number(obj[labelName].length) ? Number(obj[labelName].length) : length
//Alert.show(obj[labelName].length);
this.typicalItem = typicalObject;
}
}

Sorry, better take off both the width and height from the field.

Related

Flex Disabled Checkbox toolTip

I need to be able to show a toolTip on disabled checkboxes. A solution I've seen here on stackoverflow and other places is to just wrap the checkbox in a Group and give the Group the toollTip. This works, but I'm trying to do this generically.
I want to be able to set a property on a custom Checkbox component and at that point wrap the Chexbox in a Group that has the toolTip set.
My problem is, I can't figure out how to add the Checkbox to a Group component at run time in the Checkbox ActionScript code. I've tried adding a showDisabledToolTip property to the Checkbox Class and when that is set do something like this:
var parent = this.parent;
var gp:Group = new Group();
gp.toolTip = this.toolTip;
gp.addElement(this);
if(parent is Group) {
parent.addElement(gp);
} else {
parent.addChild(gp);
}
My main problem at that point is this.parent is null. Besides that though, I don't even know if this will really work.
Help is appreciated. Thanks!
i came up with the solution to extend the CheckBox class and create a new CheckBoxSkin with 2 new SkinStates inside (disabledWithTooltip and disabledWithTooltipSelected)
The extended Checkbox class adds a new disabledWithTooltip property and overrides the getCurrentSkinState method and the mouseEventHandler from ButtonBase
Custom CheckBox class
package components
{
import flash.events.Event;
import flash.events.MouseEvent;
import mx.events.FlexEvent;
import spark.components.CheckBox;
[SkinState (disabledWithToolTip)]
[SkinState (disabledWithToolTipSelected)]
public class CustomCheckBox extends CheckBox
{
private var _disabledKeepToolTip:Boolean = false;
public function CustomCheckBox()
{
super();
this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete, false, 0, true);
}
protected function onCreationComplete(ev:FlexEvent):void {
//_storedState = this.currentState;
}
protected override function getCurrentSkinState():String {
if(!_disabledKeepToolTip)
return super.getCurrentSkinState();
else {
if(!selected)
return "disabledWithToolTip";
else
return "disabledWithToolTipSelected";
}
}
protected override function mouseEventHandler(event:Event):void {
var skinState:String = getCurrentSkinState();
if(skinState != "disabledWithToolTip" && skinState != "disabledWithToolTipSelected") {
super.mouseEventHandler(event);
}
}
[Bindable]
[Inspectable(category="General", enumeration="true,false", defaultValue="true")]
public function get disabledKeepToolTip():Boolean {
return _disabledKeepToolTip;
}
public function set disabledKeepToolTip(value:Boolean):void {
_disabledKeepToolTip = value;
this.invalidateSkinState();
}
}
}
Create a new Skin based on (spark) CheckBoxSkin and change the hostcomponent in the metadata
[HostComponent("components.CustomCheckBox")]
and add two new skinStates
<s:State name="disabledWithToolTip" stateGroups="disabledStates" />
<s:State name="disabledWithToolTipSelected" stateGroups="disabledStates, selectedStates" />
Usage e.g.
<s:HGroup>
<components:CustomCheckBox id="custom_chk" label="KeepTooltipCheckbox" skinClass="skins.CustomCheckBoxSkin" toolTip="See this tooltip"/>
<s:CheckBox id="enable_chk" label="enable/disable" change="{custom_chk.disabledKeepToolTip = enable_chk.selected}"/>
</s:HGroup>
You have to adapt your own package structure if it's different...

Embedding FXG document by variable name in Flex

I've been trying to get FXG to work in my Flex app, it works and renders fine but what I'm trying to accomplish is a sort of a gallery with data about the images in a database. I used to be able to use <s:Image source=/path/{variable_name}> but now I have to import the FXG files and can't use <s:Image> anymore. Here I can display a static FXG image:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fxg="assets.fxg.*"
tabBarVisible="false" title="{data.name}">
<fxg:megapicture001 x="118" y="27" width="338" height="519"/>
<s:Label x="78" y="43" text="{data.name}"/>
<s:navigationContent>
<s:Button icon="#Embed('assets/home.png')" click="navigator.popView()"/>
</s:navigationContent>
</s:View>
Trying to do <fxg:{data.picturename} /> blows up.
You can't import and use the FXG elements stand alone since they aren't display objects. My take was to wrap them in a UIComponent container. This class will probably end up as part of the Flextras Mobile Component set in our next update sometime early next year most likely:
package com.dotcomit.utils
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import mx.core.UIComponent;
public class FXGImage extends UIComponent
{
public function FXGImage(source:Class = null)
{
if(source){
this.source = source;
}
super();
}
// this will tell us the class we want to use for the display
// most likely an fxgClass
private var _source : Class;
protected var sourceChanged :Boolean = true;
public function get source():Class
{
return _source;
}
public function set source(value:Class):void
{
_source = value;
sourceChanged = true;
this.commitProperties();
}
public var imageInstance : DisplayObject;
// if you want to offset the position of the X and Y values in the
public var XOffset :int = 0;
public var YOffset :int = 0;
// if you want to offset the position of the X and Y values in the
public var heightOffset :int = 0;
public var widthOffset :int = 0;
override protected function createChildren():void{
super.createChildren();
if(this.sourceChanged){
if(this.imageInstance){
this.removeChild(this.imageInstance);
this.imageInstance = null;
}
if(this.source){
this.imageInstance = new source();
this.imageInstance.x = 0 + XOffset;
this.imageInstance.y = 0 + YOffset;
this.addChild(this.imageInstance);
}
this.sourceChanged = false;
}
}
override protected function commitProperties():void{
super.commitProperties();
if(this.sourceChanged){
// if the source changed re-created it; which is done in createChildren();
this.createChildren();
}
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(unscaledHeight != 0){
this.imageInstance.height = unscaledHeight + this.heightOffset;
}
if(unscaledWidth != 0){
this.imageInstance.width = unscaledWidth + this.widthOffset;
}
}
}
}
You can use it something like this:
<utils:FXGImage id="fxgImage" source="assets.images.mainMenu.MainMenuBackground" height="100%" width="100%" />

Datagrid selects the wrong custom cell in my datagrid

I am working on a problem since a week soon, but I still couldn't make it work as expected. I have a DataGrid which has HBox with a CheckBox an a Label as itemRenderer (see Code below). When I tap in to the Cell the standard itemEditor pops up and lets you enter the content of the label. Thats the standard behavior. I works fine except for 2 problems:
If I enter to much text, the horizontal srollbar pops up, and the cell is filled with that scrollbar. As you see I tried to set the horizontalScrollPolicy to off, but that doesnt work at all... I tried to do that for all the different elements, but the failure is still existent.
When I have filled more than one row, there is an other mistake happening. If I tap on a row, the datagrid selects the one below that row. That's only if one line is already selected. If I tap outside the datagrid and then, tap at any row the itemEditor of the right row will show up... Is there anything now wright in the setup of my set data method?
__
package components
{
import mx.containers.HBox;
import mx.controls.CheckBox;
import mx.controls.Label;
public class ChoiceRenderer extends HBox
{
private var correctAnswer:CheckBox;
private var choiceLabel:Label;
public function ChoiceRenderer()
{
super();
paint();
}
private function paint():void{
percentHeight = 100;
percentWidth = 100;
setStyle("horizontalScrollPolicy", "off");
super.setStyle("horizontalScrollPolicy", "off");
correctAnswer = new CheckBox;
correctAnswer.setStyle("horizontalScrollPolicy", "off");
addChild(correctAnswer);
choiceLabel = new Label;
choiceLabel.setStyle("horizontalScrollPolicy", "off");
addChild(choiceLabel);
}
override public function set data(xmldata:Object):void{
if(xmldata.name() == "BackSide"){
var xmlText:Object = xmldata.TextElements.TextElement.(#position == position)[0];
super.data = xmlText;
choiceLabel.text = xmlText.toString();
correctAnswer.selected = xmlText.#correct_answer;
}
}
}
Thanks in advance!
Markus
I am not sure if this is the reason behind your issues, but the standard way of creating children is to override the createChildren method.
Also, you are missing an else statement - you are not calling super.data when if condition fails. That doesn't look good either.
Try:
package components
{
public class ChoiceRenderer extends HBox {
private var correctAnswer:CheckBox;
private var choiceLabel:Label;
public function ChoiceRenderer() {
super();
percentHeight = 100;
percentWidth = 100;
setStyle("horizontalScrollPolicy", "off");
}
override protected function createChildren():void {
super.createChildren();
correctAnswer = new CheckBox();
addChild(correctAnswer);
choiceLabel = new Label();
choiceLabel.setStyle("horizontalScrollPolicy", "off");
addChild(choiceLabel);
}
override public function set data(xmldata:Object):void {
if(xmldata.name() == "BackSide") {
var xmlText:Object = xmldata.TextElements.TextElement.(#position == position)[0];
super.data = xmlText;
choiceLabel.text = xmlText.toString();
correctAnswer.selected = xmlText.#correct_answer;
}
else {
//what if xmldata.name() is not "BackSide"?
//you are not calling super.data in that case
}
}
}
in order to avoid scroll bar you have to let datagrid have variable height
<mx:DataGrid id="dg"
dataProvider="{dp}"
variableRowHeight="true"
creationComplete="dg.height=dg.measureHeightOfItems(0,dp.length)+dg.headerHeight+2"/>

Flex tree expand and collapse

I have created a tree with a custom treeitemrenderer where I have added a textinput box next to each label. My data provider is an ArrayCollection.
When I enter value in the input box, I can see the value fine as I expand or collapse the tree using the disclousure arrow. There is a strange issue when I try to expand and collapse tree from the mxml page with a button click to expand and collapse tree using the following code.
The problem is as I expand or collapse tree, the values I entered in the input boxes appear to be moving.
For example, I have entered 15 in input box 1. When I expand and collapse tree, 15 moves to another input box, and moves to another, and then it moves back to where it was entered as I keep expanding and collapsing the tree. I am still learning Flex. I am not sure what is happening, it is something to do with arraycollection.
Any help would be greatly appreciated. Thanks.
private function expandTree():void{
for (var i:int = 0; i < thisTree.dataProvider.length; i ++){
thisTree.expandChildrenOf(thisTree.dataProvider[i], true)
}
}
private function collapseTree():void{
for (var i:int = 0; i < thisTree.dataProvider.length; i ++){
thisTree.expandChildrenOf(thisTree.dataProvider[i], false)
}
}
Flex item renderers are recycled, which is what is causing your data to walk like this. In your item renderer, I generally find it is useful to cast your data to a bindable value object and override the setter for data.
[Bindable]
private var myBindableValueObject:MyBindableValueObject;
override public function set data(v:Object):void
{
if(v == data)
return;
myBindableValueObject = v as MyBindableValueObject;
super.data = value;
validateNow();
}
You will need to bind the properties of the text input and labels to values in the value object. This will make sure the proper values are displayed at the appropriate location. Using this approach should eliminate the oddities that you are experiencing.
I tried as you suggested but I don't see any changes. Not sure I was able to follow your suggestion. Here is the code. Thanks.
package
{
import mx.collections.*;
import mx.controls.Label;
import mx.controls.TextInput;
import mx.controls.listClasses.*;
import mx.controls.treeClasses.*;
[Bindable]
public class TestSetupTreeItemRenderer extends TreeItemRenderer {
[Bindable]
public var _numQuestion:TextInput;
private var _numOfQuestionText:Label;
private var _listData:TreeListData;
[Bindable]
public var tItem:TestSetupTreeItem;
public function TestSetupTreeItemRenderer():void {
super();
mouseEnabled = false;
}
override protected function createChildren():void {
super.createChildren();
_numQuestion = new TextInput();
_numQuestion.text = "0"; //default
_numQuestion.width = 45;
_numQuestion.height = 20;
_numQuestion.restrict = "0123456789";
addChild(_numQuestion);
_numOfQuestionText = new Label();
_numOfQuestionText.width = 60;
_numOfQuestionText.height = 22;
addChild(_numOfQuestionText);
}
override public function set data(value:Object):void {
if(value == data)
return;
tItem= value as TestSetupTreeItem;
super.data = value;
validateNow();
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth,unscaledHeight);
if(super.data){
tItem = super.data as TestSetupTreeItem;
this.label.width = 150;
this.label.truncateToFit(null);
this.label.multiline = true;
this.label.wordWrap = true;
var tld:TreeListData = TreeListData(super.listData);
this._numOfQuestionText.text = "of " + tItem.totalQuestionCount;
this._numOfQuestionText.x = 300;
this._numOfQuestionText.y = super.label.y;
this._numQuestion.x = 200;
this._numQuestion.y = super.label.y;
this._numQuestion.editable = true;
tItem.desiredQuestionCount = this._numQuestion.text;
}
}
}
}

Flex: Combobox loses is focus when data-provider updated?

It seems that ComboBoxes loose their selected item after their dataProvider updates, even if the same selected item is still in the dataProvider. They convert back to the first item being selected. Is there anyway to prevent this? So that if the same object is in the dataProvider it keeps the same object selected and only reverts back to the first index if the selected object is not in the updated dataProvider?
Thanks!
If the ComboBox looses its selected item, it means the dataProvider isn't updated - it is replaced. If you bind a ComboBox to an ArrayCollection and then add an item to the AC, The ComboBox is updated without loosing its selectedItem.
Sometimes you have to replace the dataProvider and in those cases, you have to listen for the updateComplete-event and reset the selectedItem. You can try this code:
<mx:Script>
<![CDATA[
import mx.controls.ComboBox;
import mx.events.ListEvent;
import mx.events.FlexEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var dp:ArrayCollection = new ArrayCollection(["Item 1", "Item 2", "Item 3"]);
private var selectedItem:*;
private var dataProvider:*;
private function onChange(event:ListEvent):void {
selectedItem = (event.currentTarget as ComboBox).selectedItem;
}
private function onUpdateComplete(event:FlexEvent):void {
trace(event);
var cb:ComboBox = event.currentTarget as ComboBox;
if(dataProvider == null || cb.dataProvider != dataProvider) {
if(selectedItem != null && cb.selectedItem != selectedItem) cb.selectedItem = selectedItem;
if(cb.selectedIndex < 0) cb.selectedIndex = 0;
dataProvider = cb.dataProvider;
}
}
private function extendDP():void {
dp.addItem("Item " + (dp.length +1));
var ac:ArrayCollection = new ArrayCollection(dp.source);
dp = ac;
}
private function reduceDP():void {
dp.removeItemAt(dp.length -1);
var ac:ArrayCollection = new ArrayCollection(dp.source);
dp = ac;
}
]]>
</mx:Script>
<mx:VBox>
<mx:ComboBox dataProvider="{dp}" change="onChange(event)" updateComplete="onUpdateComplete(event)" />
<mx:Button label="Extend dp" click="extendDP()" />
<mx:Button label="Reduce dp" click="reduceDP()" />
</mx:VBox>
It creates a ComboBox and binds it to an ArrayCollection. The two buttons adds and removes items from the collection.
Well I was able to extend the ComboBox with this class, which just looks for the selected label and compares with labels from the new dataProvider. It seems to work, although kinda ad-hoc. I was hoping for a scalable solution.
package
{
import mx.controls.ComboBox;
import mx.collections.ArrayCollection;
public class SelectionKeepingComboBox extends ComboBox
{
public function SelectionKeepingComboBox()
{
super();
}
override public function set dataProvider(value:Object):void
{
var curSelectedLabel:String;
if(this.selectedItem)
{
curSelectedLabel = this.selectedLabel;
}
super.dataProvider = value;
if(curSelectedLabel == null)
{
return;
}
var dp:Array;
if(this.dataProvider is ArrayCollection)
{
dp = this.dataProvider.toArray();
}
else
{
dp = this.dataProvider as Array;
}
for(var i:uint = 0; i<dp.length; i++)
{
var obj:Object = dp[i];
var dpLabel:String = obj.label;
if(dpLabel == curSelectedLabel)
{
this.selectedItem = obj;
}
}
}
}
}

Resources