Ask a Question related to Macromedia Flex General Discussion, Design and Development.
-
agh1 #1
Java Object Array to DataGrid
Hello,
I'm a FLEX newbee and have a quick question re: passing information back to my
FLEX application from my existing JAVA classes. For testing, I created a
simple Java class that returns an Object array of names and postions. I use
the Remote Object syntax and return the result to an Array var within an
ActionScript function. A couple of questions:
- How do I cycle thru this data and add it to a datagrid? When I set the data
provider attribute of the datagrid equal to the array nothing displays (I'm
guessing this incorrect). What are the different ways to add information to
the datagrid dynamically?
- I know it would be more effective to create a data model but how do I get my
information out of my java class and into a data model? (Stupid question but
am really having trouble figuring this out)
- Lastly, I've seen a lot of information on creating AS classes to mimic
existing java classes when sending information but I haven't seen a lot on how
to send custom Java Objects back to my Flex App.
Again, I'm really new to this so ANY help you could give me or suggestions to
my above questions would be great!
Thanks in advance!
Andy
agh1 Guest
-
Can't access remote java object
I'm using Flex Builder 3 plug-in and trying to build a simple demo app that uses JDBC to return results to a datagrid. However, I getting the... -
converting problem java object to mx dataGrid...
what fix this problem?... get java object to EJB object ..and mx:dataGrid convert but Java Object inside not Promitive class abc.def don't... -
JAVA Object to FLEX App
First, sending a Java object back to Flex and having it turn into an ActionScript object for which you have defined an ActionScript class is not all... -
webservice.htc and custom object array. only first object is deserialized in the result.value
Hello I've been using webservice.htc for 6 months or so with great results. recently i came into the following problem that I am not sure how to... -
calling java object from cf
Hi there, I am trying to call a java object from cf script but its not working. I know the class should be in web_root/WEB-INF/classes folder and... -
bdeen #2
Re: Java Object Array to DataGrid
On your datagrid did you create DataGridColumns? these allow you to add
columns and set the name of the attribute each datagrid column should map to.
you might try a
for(variableIterant in object){
statement(s);
}
This can be found in the flex_aslr.pdf document that's part of the Flex
documentation. You should probably take a day or so and read the documentation.
the java class if not registered to an actionscript class should come back as
a dynamic object, meaning a base class object that you can then reference the
properties via obj["propertyName"].
To convert an actionscript class to a java class you need to do a bit of
registering between the actionscript class and the java class as in
class com.Product
{
public var id:Number;
public var name:String;
public var price:Number;
public var description:String;
public static var regClass = Object.registerClass("com.Product",
com.Product);
function Product()
{
}
function toString()
{
return "id = " + id + " name = " + name + " price = $" + price;
}
}
which i got again from the documentation this one is from the
flex_dev_apps.pdf document.page 679.
bdeen Guest
-
agh1 #3
Re: Java Object Array to DataGrid
Thanks for your response. I apologize, I'm still a little confused. Do I need
to set up columns before I add rows to the datagrid or can I include that
information when I set the dataProvider? The method of my class is simply
returning an Object[] of information ( ie: Object[] o = {1,"test1",
2,"test2"} ) -- a simple array. Here's what I'm attempting on the FLEX side:
<!--Remote Object Definition-->
<mx:RemoteObject id="test" source="package.SomeClass">
<mx:method name="someMethod" result="resultHandler(event.result)"/>
</mx:RemoteObject>
<!--AS-->
<mx:Script>
var testArr:Array;
function resultHandler(result){
testArr=result;
//first attempt - failed
dataGrid.dataProvider = testArr;
}
</mx:Script>
Second attempt. I tried to reference the array directly in the datagrid
itself - failed. Do I need columns first (like you mentioned)??
<mx:DataGrid id="dataGrid" dataProvider="{testArr}"></mx:DataGrid>
Do I need to cycle thru my array and add the information to the DG row by row?
Also, you mentioned that a "java class, if not registered to an actionscript
class, should come back as a dynamic object" - does this mean that I simply
need to import the proper package in my AS and instantiate accordingly?
Again, thanks for your patience and help!
agh1 Guest
-
ntsiii #4
Re: Java Object Array to DataGrid
*Both of your attempts are valid, and should work. I suspect your result is not
exactly what you think.
* For any thing but the simplest case, you should declare your dataGridColumns
in the mxml. This will permit the use of a labelFunction if necessary
* You should not have to iterate through your data unless you need to
specially process it client-side.
* use the mx.utils.ArrayUtils.toArray() function around your dataProvider.
This fixes a problem with flex not knowing the difference between an object and
an array with a single element.
* for testing purposes, iterate through your result, either in a debugger or
trace or alert in your result handler to be sure of what you have. Use the for
( i:Number = 0; i<array.length;i++) style. This will ensure you have a simple
array. The for(var key in array) style would work on an object
Tracy
ntsiii Guest
-
ntsiii #5
Re: Java Object Array to DataGrid
Looking back, I think you do not have a simple array. I am not experienced
with RemotObjects. The datagrid wants an array of objects:
myArray[0] = {name:value,name2:value2...}
myArray[1] = {name:value,name2:value2...}
...
You might have to iterate through the object and build an array. Or, if you
understand it(I don't), use bdeen's registration method.
Tracy
ntsiii Guest
-
agh1 #6
Re: Java Object Array to DataGrid
Thanks for your response Tracy. Using your example, I tried the following but
it's still not working:
<!--AS-->
var testArr:Array = new Array();
var testArr2:Array = new Array();
var counter:Number = 0;
function resultHandler(result){
//I'm returning a 3 element Object[]
testArr=result;
for (var i : Number = 0;i<testArr.length;i+=3){
testArr2[counter] = [{FLD1:testArr, FLD2:testArr[i+1],
FLD3:testArr[i+2]}];
counter++;
}
}
<!--MX-->
<mx:DataGrid id="dataGrid2" dataProvider="{testArr2}">
<mx:columns>
<mx:Array>
<mx:DataGridColumn columnName="FLD1" headerText="FLD1" width="220"/>
<mx:DataGridColumn columnName="FLD2" headerText="FLD3" width="220"/>
<mx:DataGridColumn columnName="FLD3" headerText="FLD3" width="220"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>
Nothing populates in the DG. I tried to use a plain string with one element
in it and that worked ..... I think I'm close! Any further assistance you
could provide me would be great!! Thanks In advance for all of your help.
Andy
agh1 Guest
-
agh1 #7
Re: Java Object Array to DataGrid
Second datagrid column is actually:
<mx:DataGridColumn columnName="FLD2" headerText="FLD2" width="220"/>
agh1 Guest
-
ntsiii #8
Re: Java Object Array to DataGrid
We need to know exactly what yo have in the result object, and we must be very
clear on our terminology. An object array is not necessarily the same as an
array of objects, which is what we want. In AS arrays are themselves objects,
and objects can be treated lik arrays in some ways, so it is a bit messy.
Do you know exatly what you have in result? What are you using for debugging?
If you are using flex builder, you can put a break in the result handler and
examine the structure of your object. Or use the net connection debugger, or
something else.
Do you know exactly what the structure is on the Java side?
Also, be certain you have at more than one "row" in you result. We can handle
a single row but need to do a little extra work.
I am not a Java/RemoteObject guy, but the ideal structure would be something
like this, in AS psuedo-code:
class myObject
var myItems:Array;
myItems[0]= new Object({name:"Tracy",position:"developer"})
myItems[1]= new Object({name:"Andy",position:"manager"})
.....
You see, we have an ordinary, numeric-element -identified array, full of
Objects.
For a dataProvider we might then have to specify {result.myItems}, which
navigates us to the ordinary array containing the item objects. then the
datGrid columns can eaqsily specify the property they need using:
columnName="position", for example.
Tracy
ntsiii Guest
-
agh1 #9
Re: Java Object Array to DataGrid
Hi Tracy,
Thanks for the clarification. Yes, it looks like my Java class was returning
and Object array not an Array of objects. I'm using FlexBuilder and alerts to
debug. I decided to build a simple Array of Objects to just test that it
works, I'm still not able to populate the DG. Here's what I tried:
<!--AS-->
var myItems:Array = new Array();
function resultHandler(result){
var test:Object = new Object({name:"Tracy",position:"developer"});
var test2:Object = new Object({name:"Andy",position:"manager"});
myItems[0] = test;
myItems[1] = test2;
}
<!--MX code (DataGrid)-->
<mx:DataGrid id="dataGrid2" dataProvider="{myItems}">
<mx:columns>
<mx:Array>
<mx:DataGridColumn columnName="name" headerText="Name" width="220"/>
<mx:DataGridColumn columnName="position" headerText="Position" width="220"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>
I appreciate your help Tracy! Am I on the right track?
Andy
agh1 Guest
-
ntsiii #10
Re: Java Object Array to DataGrid
Very close. You are simulating the result event of a data service, but you are
not actually calling that result handler function. In the sample app below, I
used the initialize event on the datagrid to call the simulated result handler
function.
Tracy
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script><![CDATA[
var myItems:Array = new Array();
private function simulatedResultHandler():Void
{
myItems[0] = {name:"Tracy",position:"developer"};
myItems[1] = {name:"Andy",position:"manager"};
}
]]></mx:Script>
<mx:DataGrid id="dataGrid2" dataProvider="{myItems}"
initialize="simulatedResultHandler()">
<mx:columns>
<mx:Array>
<mx:DataGridColumn columnName="name" headerText="Name" width="220"/>
<mx:DataGridColumn columnName="position" headerText="Position" width="220"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>
</mx:Application>
ntsiii Guest
-
ntsiii #11
Re: Java Object Array to DataGrid
By the way, you should get the flashlog file working so you can use trace
statements. Alerts are just barely ok, you will like tracing much better.
Directions on how to do this are in the "Developing flex apps" documentation.
Tracy
ntsiii Guest
-
agh1 #12
Re: Java Object Array to DataGrid
Your example worked great! I'm almost there! I'm sorry Tracy, I didn't
include my remote object code definition that I reference:
<!--Remote Object Definition -->
<mx:RemoteObject id="auth" source="somePackage.Authenticate"> <mx:method
name="returningUserLogIn" result="resultHandler(event.result)"/>
</mx:RemoteObject>
<!--AS -->
function resultHandler(result){
//Where I'd like my AS code to read the Array returned in the result and
populate the DG. Right now I'm just using your sample Array
//psuedo-code
Array = result;
Dataprovider = Array;
login.txtUN.text="";
login.txtPW.text="";
}
function doLogin(name:String,password:String):Void {
auth.returningUserLogIn(name,password);
}
When the user enters their credentials, they will click on a login button.
This will call the doLogin method which calls my Java method, which returns the
results which populate the DG (eventually). What if I do not want to populate
the DG when intialized but rather populate it after the user clicks on a button
(ie, the Login button I just referenced above)?? Is there another DG attribute
that I need to define to do this?? Thanks Tracy!!
agh1 Guest
-
agh1 #13
Re: Java Object Array to DataGrid
Hi Tracy, THANKS so much for your help! I was able to figure it out. I'm sure I'll have many more questions down the road ... thanks again for you patience!
Andy
agh1 Guest
-
ntsiii #14
Re: Java Object Array to DataGrid
What will happen is that your doLogin will make the RemoteObject call, which
will then run the resultHandler when it returns.
By the way, I believe you need to send() your RemoteObject request. I don't
see you doing that. Are you sure you are getting a result event being fired?
Don't focus on that sample app, it really just shows how to build and
reference a dataProvider consisting of an array of objects.
We are back at the point where we need to know exactly what is the structure
of the data in the returned result object.
Can you use the FlexBuilder debugger to examint that object once it gets into
the resultHandler?
Tracy
ntsiii Guest
-
ntsiii #15
Re: Java Object Array to DataGrid
Ah, good, our posts just missed each other.
Happy to help.
Tracy
ntsiii Guest
-
Unregistered #16
Java Object Array to DataGrid
I have similar question where the resultset from java is dynamic so the number of columns can vary. How to populate datagrid with this where java side returning something like List<List<Object>> or ArrayList<ArrayList<Object>> ?
A concrete example on how to do this would be extremely helpful.Unregistered Guest



Reply With Quote

