# How to use Java ArrayList in ZK

Faabass
6

Hello, i'm doing my first proyect in zk but i'm having some problems.

The most important is how to use the object that i have in java code like ArrayList<person> in Zk.
For Example, i have the class object that have nameParson and idPerson, with theis getter and setter (getName, setName, getId, setId)
And i have a java method that return me an ArrayList<person>.

How can i do a combobox, for example, where the Id of the item will be the person id, and de value the person name.

I did the following but it didn'y work.

In java

	public ArrayList<entity.Person> getAllPeople()
{
negocio.PersonB oNegocio = new negocio.PersonB();
return oNegocio.getAllPeople();
}


In Zk

	<zscript><![CDATA[
import java.util.ArrayList;
presentacion.Person oPresentacion = new presentacion.Person();
ArrayList<entity.Person> oPeople = new ArrayList<entity.Person>();
oPeople = oPresentacion.getAllPeople();
]]></zscript>
<combobox id="box" width="200px" mold="rounded" model="${oPeople}" > <comboitem self="@{each='per'}"> <id value="${per.idPerson}" />
<label value="${per.namePerson}" /> </comboitem> </combobox>  Thanks! delete retag edit ## 5 Replies Sort by » oldest newest answered 2011-04-29 02:04:36 +0800 mjablonski 1284 3 5 http://www.jease.org/ The classical BeanShell-Interpreter is used to interpret Java code contained in ZK-scripts. Unfortunately classical BeanShell doesn't support generics. There's a fork of the classical BeanShell called BeanShell2 which seems to have added support for generics: http://code.google.com/p/beanshell2/ Maybe you can switch the bsh.jar from your project with the one from Beanshell2 and see if it works. On the other hand I would recommend to use Richlets for enabling the full power of Java in combination with ZK: http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/UI_Composing/Richlet Cheers, Maik answered 2011-04-29 02:49:14 +0800 Steva77 1014 3 http://www.research.softe... BTW - shouldn't the model use a ListModelList or something similar? answered 2011-04-29 07:10:27 +0800 caclark 1753 2 5 http://clarktrips.intltwi... Give this a try: <zscript><![CDATA[ import java.util.ArrayList; presentacion.Person oPresentacion = new presentacion.Person(); ArrayList<entity.Person> oPeople = new ArrayList<entity.Person>(); oPeople = oPresentacion.getAllPeople(); ListModel dataModel = new SimpleListModel(data); ]]></zscript> <combobox id="box" width="200px" mold="rounded" model="${dataModel}" >
<comboitem self="@{each='per'}">
<label value="${per.id}" /> <label value="${per.name}" />
</comboitem>
</combobox>



As @Steva77 noted, you need a ListModelList. I have not glanced at the source code in awhile, but the combobox might wrap the ArrayList for you, but we'll remove that doubt.
Also, the values for your labels will be turned into getters. I'm not sure you had Person::getIdPerson and Person::getNamePerson given your earlier description of the methods.
And, lastly, I hope you eventually move the code in the zscript to a Controller...zscript is good for testing, not so much for production. Use the MVC paradigm; it's your friend.

Faabass
6

Thanks a lot for your help!
@caclark or someone, could you give me and example of this or any page where i could find more information about how to move the code to a controller and how to use the MVC paradign?

PeterKuo
481 2

@Faabass

[hide preview]