Wednesday, June 18, 2008

Where to put your removeEventListeners?

Everyone tells you to make sure to execute a removeEventListener for each addEventListener but where do you put this code? Most people put their addEventListeners in the creationComplete event but there's no destructionComplete event...

There are 2 options:
  • Or you don't use removeEventListener and make sure that you use a weak reference when doing addEventListener (setting the 5th parameter of addEventListener to true)
  • Or you attach event listeners in the "addedToStage" event and you remove them again in the "removedFromStage" event

Why Adobe didn't set the 'weak reference' parameter by default to 'true' is unclear to me. The current situation is probably one of the major causes of memory leaks in Flex applications...

Degrafa component not showing up?

There seems to be some problem when building a Degrafa component in MXML. It seems that using "Surface" as the outer container results in an invisible component. The workaround is simple: put a Canvas around it but don't ask me why :)

Tuesday, May 27, 2008

Dynamic (Advanced)DataGrid columns

Trying to add columns dynamically to your DataGrid or AdvancedDataGrid and having trouble with specifically setting the width of a column?

Try the following:

var cols: Array = datagrid.columns;
cols.push(newColumn);
datagrid.columns = cols;
// The following line is very important
// (otherwise the grid won't be aware of the new columns yet)
datagrid.validateNow();
// Force the width of a specific column
cols[0].width = 40;

Thursday, May 15, 2008

Reducing the space between the legend marker and the legend label

If you ever try to reduce the space between the legend markers and the legend labels:

LegendItem
{
horizontalGap: 0;
verticalGap: 0;
}

Monday, April 21, 2008

Deep object compare

Ever wanted to check in Flex whether all the public members of an object are the same? You can use the ObjectToString method in that case:

public class SerializeUtil
{
public static function ObjectToString(object: *): String
{
var ba: ByteArray = new ByteArray();
ba.writeObject(object);
return ba.toString();
}
public static function ObjectToByteArray(object: *): ByteArray
{
var ba: ByteArray = new ByteArray();
ba.writeObject(object);
ba.position = 0;
return ba;
}
}

To do the actual comparison:

if (SerializeUtil.ObjectToString(object1)==SerializeUtil.ObjectToString(object2))
{
// Objects are 'equal'
}
else
{
// Objects are not 'equal'
}

Thursday, February 14, 2008

ChartItemEvent.CHANGE in conflict with IndexChangedEvent

Earlier this week I ran into a strange problem when the ChartItemEvent.CHANGE event is raised. It seems that the string behind the constant is 'change' and this happens to be the same as the string behind the IndexChangedEvent.CHANGE event. Probably programmed on monday morning :)

Fortunately there's a simple workaround:
Put a container around your 'problematic' charting component (for example a canvas) and attach a listener to the ChartItemEvent.CHANGE event to stop it from propagating.

<mx:Canvas id="canvas" creationComplete="canvasInit()">
  <mx:PieChart id="pieChart" ... />
</mx:Canvas>

// Hack to prevent propagation of the ChartItemEvent.CHANGE as it is in conflict with IndexChangedEvent.CHANGE
private function canvasInit(): void
{
canvasInit.addEventListener(ChartItemEvent.CHANGE, chartItemEventChange, true, 0, true);
}

private function chartItemEventChange(event: Event): void
{
event.stopImmediatePropagation();
}

Tuesday, January 29, 2008

Is your PopupButton suffering from Alzheimer?

Recently I noticed that my PopupButton didn't want to popup anymore... Sometimes it worked and most of the time it didn't. The problem is related to a 'fix' Adobe did to get rid of a memory leak. The 'fix' may have fixed the memory leak but introduced serious trouble. Scott Melby seems to have found a workaround that works most of the time. More information on http://tech.groups.yahoo.com/group/flexcoders/message/99105. Let's hope this gets fixed soon because it's clearly a bad fix.