How to make text color change according to the data recieved by javaside. In need of some references! Thanks alot
i tried out some line but it just didnt work
If (ChatClient.varfield[5] == 1)
{
text.color= 0x000000
}
Been trying alot of times, Is there any solution out there?
1. make sure you're receiving the javascript data. the most common problem with using the addCallback() method of the externalinterface class is incorrect embedding code. either use swfobject to embed or use the embedding code in the help files.
2. the code you showed appears to have nothing to do with retrieving and using data from javascript. show something that appears germane to those of us unfamiliar with your code.
Above code was an example
Here is the code in my fla file to recieve data from javaside in the grid
var myDataGridemotion:DataGrid = new DataGrid();
myDataGridemotion.addColumn("Phase");
myDataGridemotion.addColumn("Speed");
myDataGridemotion.addColumn("Direction");
myDataGridemotion.dataProvider = ChatClient.dpmotion;
myDataGridemotion.headerHeight = 30;
myDataGridemotion.rowHeight = 40;
myDataGridemotion.width = 250;
myDataGridemotion.height = 150;
myDataGridemotion.sortableColumns = false;
myDataGridemotion.move(30, 115);
myDataGridemotion.setRendererStyle("textFormat", tahomaTF);
//myDataGridemotion.scrollToIndex(myDataGridemotion.length-1);
addChild(myDataGridemotion);
this is the code in my action script file that grab data from javascript
public class ChatClient extends EventDispatcher
{//extends Sprite {
private var host:String;
private var port:int;
private var socket:Socket;
public static var varfield:Array = new Array();
public static var e2eTime:Array = new Array();
public static var dpP2P:DataProvider = new DataProvider();
public static var dpe2e:DataProvider = new DataProvider();
public static var dpmotion:DataProvider = new DataProvider();
private var myTween:Tween;
public function ChatClient(h:String, p:int)
{
this.host = h;
this.port = p;
//this.chatArea = ca;
}
private function receiveData(msg:String):void
{
if (varfield.length > 1)
{
if (varfield[1] == "Link")
{
//var tmpstr = varfield[2] + "-" + varfield[3];
//var tmpstr1 = varfield[5] + " ms";
//var tmpstr2 = varfield[7] + " dBm";
//trace("Link Type : ");
//trace(parent.name);
//if (dpP2P.length >= 50)
//{
// dpP2P.removeItemAt(0);
//}
//dpP2P.addItem({Link:tmpstr, Link_Qty:varfield[4], Delay:tmpstr1, RSSI:varfield[6], RF_Power:tmpstr2});
dispatchEvent(new Event("onLinkData"));
}
if (varfield[1] == "Status")
{
//trace("Status Type : ");
dispatchEvent(new Event("onStatusData"));
}
if (varfield[1] == "Motion")
{
trace("Motion Type : ");
if (dpmotion.length >= 3) //= 50)
{
//dpmotion.removeItemAt(0);
dpmotion.replaceItemAt(dpmotion.getItemAt(1), 0);
//dpmotion.addItemAt(
//dpmotion.removeItemAt(1);
dpmotion.replaceItemAt(dpmotion.getItemAt(2), 1);
//dpmotion.removeItemAt(2);
dpmotion.replaceItemAt({Phase:varfield[3], Speed:varfield[4], Direction:varfield[5]}, 2);
}else{
dpmotion.addItem({Phase:varfield[3], Speed:varfield[4], Direction:varfield[5]});
}
Currently i can recieve the data properly just that i want to set the text color change according to the data recieved . For instance 0-20 the data will appear to be red, 21-40 it will appear to be black.
i can't understand why you would post the 1st or 2nd code snippets. there's nothing (other than a suggestive name, receiveData) about retrieving data from javascript.
anyway, if you understand your code, you should be able to create additional textformat instances that use the colors you want and apply those to your datagrid (assuming that's the object whose text color you want to change) using setRendererStyle just like you showed in the 2nd snippet.
for example:
if (varfield[1] == "Link")
{
dispatchEvent(new Event("onLinkData"));
myDataGridmotion.setRendererStyle("textFormat",tfor1);
} else if (varfield[1] == "Status")
{
myDataGridmotion.setRendererStyle("textFormat",tfor2);
dispatchEvent(new Event("onStatusData"));
} else if (varfield[1] == "Motion")
{
myDataGridmotion.setRendererStyle("textFormat",tfor3);
}
First of all i got one question. Must i code it in my FLA or AS file?
Secondly, i would like to know how to create a more specific one as in i want a certain data text to change color only like data in direction:varfield[5], so how should i code?
Can i code it this way?
if(varfield[5] == 1)
{
myDataGridmotion.setRendererStyle("textFormat",tfor1);
}
else if (varfield[5] == 2)
{
myDataGridmotion.setRenderStyle("textFormat",tfor2);
}
Please guide me. Thanks
For what you've posted abve, i tried to type the above code in Snippets 2 and got an error of undefined property for myDataGridemotion
i used myDataGridmotion and you were using myDataGridemotion. so, you should have seen an error about myDataGridmotion being undefined, not myDataGridemotion.
in any case, they should be all one or the other.
next, if you're trying to control the format of individual cells in your datagrid, you will need to use a custom cell renderer: http://gourry-gabrief.blogspot.com/2009/06/how-to-customize-cell-in-da tagrid-with.html
Thanks for sharing. Anyway i'm still stuck at it.
I followed the steps in the blog that you given me but it can't work
I created 2 action script file
CustomCellRenderer.as
package {
import fl.controls.listClasses.CellRenderer;
import fl.controls.listClasses.ICellRenderer;
import RedColor
public class CustomCellRenderer extends CellRenderer implements ICellRenderer {
public function CustomCellRenderer():void {
super();
}
public static function getStyleDefinition():Object {
return CellRenderer.getStyleDefinition();
}
override protected function drawBackground():void {
if (_data)
if (_data.Speed> 1) { setStyle("upSkin", RedColor ); }
super.drawBackground();
}
}
}
RedColor.as
package {
import flash.display.Sprite;
public class RedColor extends Sprite {
private var _rectangle:Sprite;
public function RedColor() {
_rectangle.graphics.beginFill(0XFF0000,1);
_rectangle.graphics.drawRect(0,0,100,20);
_rectangle.graphics.endFill();
addChild(_rectangle);
}
}
}
in my Fla file
var nameColumn:DataGridColumn = new DataGridColumn("Speed");
nameColumn.cellRenderer = CustomCellRenderer ;
myDatacan runGridemotion.addColumn(nameColumn);
It can be exported to SWF properly, but whenever the data come in i will get error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at RedColor()[C:\Users\IA\Desktop\FlashFiles-I2R\RedColor.as:9]
at fl.core::UIComponent/getDisplayObjectInstance()
at fl.controls::BaseButton/drawBackground()
at CustomCellRenderer/drawBackground()[C:\Users\IA\Desktop\FlashFiles-I2 R\CustomCellRenderer.as:19]
at fl.controls::LabelButton/draw()
at fl.core::UIComponent/drawNow()
at fl.controls::DataGrid/drawList()
at fl.controls::DataGrid/draw()
at fl.core::UIComponent/callLaterDispatcher()
Hope you can help me with it. Thanks in advance
North America
Europe, Middle East and Africa
Asia Pacific