NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

How to populate a jsf table with an array?

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
goodvibrato



Joined: 13 Nov 2008
Posts: 8

PostPosted: Thu Nov 13, 2008 10:16 am    Post subject: How to populate a jsf table with an array? Reply with quote

I have a JSF project where I'm using a table and I would like to populate that table with some custom information without using a database. I'm trying to write my own data provider. I was wondering if anyone knows how to populate a jsf table using an array. Any help would be appreciated. Thanks.
Back to top
Futaleufu_John
Posted via mailing list.





PostPosted: Thu Nov 13, 2008 1:37 pm    Post subject: How to populate a jsf table with an array? Reply with quote

Use the ObjectListDataProvider. The following blog entry provides a
workaround for a bug in the data provider.

http://blogs.sun.com/winston/entry/objectlistdataprovider_workaround
http://blogs.sun.com/winston/entry/objectlistdataprovider_workaround

goodvibrato wrote:
Quote:

I have a JSF project where I'm using a table and I would like to populate
that table with some custom information without using a database. I'm
trying to write my own data provider. I was wondering if anyone knows how
to populate a jsf table using an array. Any help would be appreciated.
Thanks.


--
View this message in context: http://www.nabble.com/How-to-populate-a-jsf-table-with-an-array--tp20478379p20480936.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
Back to top
goodvibrato



Joined: 13 Nov 2008
Posts: 8

PostPosted: Fri Nov 14, 2008 2:47 am    Post subject: Reply with quote

Thanks for the reply I'll check that stuff out.
Back to top
goodvibrato



Joined: 13 Nov 2008
Posts: 8

PostPosted: Sat Nov 15, 2008 11:08 am    Post subject: Reply with quote

Hey I looked at the tutorials, but it's using Creator 2 not Netbeans. If anyone knows how to populate a jsf table without using a database I would be very grateful.
Back to top
Rick Fincher
Posted via mailing list.





PostPosted: Sat Nov 15, 2008 6:48 pm    Post subject: How to populate a jsf table with an array? Reply with quote

It's basically the same. Creator was merged with Netbeans several years
ago.

Rick

goodvibrato wrote:
Quote:
Hey I looked at the tutorials, but it's using Creator 2 not Netbeans. If anyone knows how to populate a jsf table without using a database I would be very grateful.



Back to top
Futaleufu_John
Posted via mailing list.





PostPosted: Sat Nov 15, 2008 6:51 pm    Post subject: How to populate a jsf table with an array? Reply with quote

I migrated from Creator to NetBeans about a year ago. The Creator tutorials
should work for NetBeans too as the two platforms share many of the same
features.

I say this because my Creator web app used object list data providers and
when I migrated to NetBeans I didn't have to change anything. So I'm
confident that the Creator tutorials regarding that are still valid.

Hope this helps.

goodvibrato wrote:
Quote:

Hey I looked at the tutorials, but it's using Creator 2 not Netbeans.

--
View this message in context: http://www.nabble.com/How-to-populate-a-jsf-table-with-an-array--tp20478379p20518403.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
Back to top
goodvibrato



Joined: 13 Nov 2008
Posts: 8

PostPosted: Sun Nov 16, 2008 3:19 am    Post subject: Reply with quote

Yeah, I'm guessing that tutorial is a little dated because I tried right clicking on the SessionBean1.java, but there wasn't an option to add a property and I don't know Netbeans well enough to find another way. I did manage to populate the table with the setValue() method of the defaultTableDataProvider so that will work for now. Thanks anyway.
Back to top
Don Millhofer
Posted via mailing list.





PostPosted: Sun Nov 16, 2008 3:33 pm    Post subject: How to populate a jsf table with an array? Reply with quote

Hi goodvibrato, not sure if you are using VWP but this is how I would do it if you are:

Create a custom ObjectListDataProvider, define it in the SessionBean1 with getter and setter, and when you drag and drop a dataTable onto a form in the designer you should be able to bind the provider to the table using the TableLayout window, once bound you may use any of the object's properties as table columns or define your own.

Don


At 06:08 AM 11/15/2008, you wrote:
Quote:
Hey I looked at the tutorials, but it's using Creator 2 not Netbeans. If anyone knows how to populate a jsf table without using a database I would be very grateful.
Back to top
goodvibrato



Joined: 13 Nov 2008
Posts: 8

PostPosted: Mon Nov 17, 2008 1:55 pm    Post subject: Reply with quote

Hey Don, I am using Visual JSF. Not sure if that's part of VWP or not. I dragged and dropped the dataTable and made my own data provider, but I wasn't able to bind the table to the provider using the table layout option. However, I have a woodstock table and it will come up as an option in that table. Would you mind posting a code example so I can see what a custom data provider should look like? Here's what I have so far:

Code:
import com.sun.data.provider.FieldKey;
import com.sun.data.provider.impl.ObjectListDataProvider;
//import java.io.Serializable;
import java.util.ArrayList;

public class DataProvider extends ObjectListDataProvider {
   
    private FieldKey field1;
    private FieldKey field2;
    private FieldKey field3;
    private ArrayList list;
   
    public DataProvider()
    {
        list = new ArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        field1 = new FieldKey("0", "Item");
        field2 = new FieldKey("1", "Size");
        field3 = new FieldKey("2", "Quantity");
        this.addFieldKey(field1);
        this.addFieldKey(field2);
        this.addFieldKey(field3);
        setList(list);
        setIncludeFields(true);
    }
}
Back to top
Don Millhofer
Posted via mailing list.





PostPosted: Mon Nov 17, 2008 5:04 pm    Post subject: How to populate a jsf table with an array? Reply with quote

Hi goodvibrato,

Here are examples of the components - start with a simple object for the object data provider like:

package org.myOrg.myProj;

import java.io.Serializable;

public class MyObj implements Serializable {
private int myNum;
private String myString;

public MyObj() {}

public MyObj(String ms, int mn) {
myString = ms; myNum = mn;
}

public String getMyString() {
return this.myString;
}

public void setMyString(String ms) {
this.myString = ms;
}

public int getMyNum() {
return this.myNum;
}

public void setMyNum(int mn) {
this.myNum = mn;
}
}
-----------------------------------------------------------------------------------------------------------------

After the object we can create the custom ObjectListDataProvider like this:

import com.sun.data.provider.impl.ObjectListDataProvider;
import java.util.ArrayList;
import java.util.List;

public class MyOLDP extends ObjectListDataProvider {
// NOTE ObjectListDataProvider implements Serializable!
private List<MyObj>al = new ArrayList<MyObj>();

public MyOLDP() {
this.al.add(new MyObj());
setList((List<MyObj>)al);
setObjectType(MyObj.class);
List<MyObj>dataList = (List<MyObj>) (createDefaultArrayList());
al.clear();
al.addAll((List<MyObj>)dataList);

}

private List createDefaultArrayList() {
List<MyObj>me = new ArrayList<MyObj>();
me.add(new MyObj("String 1", 1));
me.add(new MyObj("String 2", 2));
me.add(new MyObj("String 3", 3));
me.add(new MyObj("String 4", 4));
al.clear();
al.addAll((List<MyObj>)me);
return (List<MyObj>)al;
}
}

----------------------------------------------------------------------------------------------

Then you define it in the SessionBean1 as follows:

import org.myOrg.myProj.MyOLDP; // Required if you used a separate package for MyOLDP

public class SessionBean1 extends AbstractSessionBean {
private MyOLDP myOLDP = new MyOLDP();

public MyOLDP getMyOLDP() {
return this.myOLDP;
}

public void setMyOLDP(MyOLDP mo) {
this.myOLDP = mo;
}

--------------------------------------------------------------------------------------------------------------

Once this is done you may have to stop and start the IDE (depending on the release you are running), then you will see the custom dataprovider in the TableLayout window as a selection option for the table you dragged and dropped on the form. Select the custom dataprovider and you will see that you can select any of it's properties as columns. Build your own logic on top of this.

Don







At 08:55 AM 11/17/2008, you wrote:
Quote:
Hey Don, I am using Visual JSF. Not sure if that's part of VWP or not. I dragged and dropped the dataTable and made my own data provider, but I wasn't able to bind the table to the provider using the table layout option. However, I have a woodstock table and it will come up as an option in that table. Would you mind posting a code example so I can see what a custom data provider should look like? Here's what I have so far:




Code:
import com.sun.data.provider.FieldKey;

import com.sun.data.provider.impl.ObjectListDataProvider;

//import java.io.Serializable;

import java.util.ArrayList;



public class DataProvider extends ObjectListDataProvider {



private FieldKey field1;

private FieldKey field2;

private FieldKey field3;

private ArrayList list;



public DataProvider()

{

list = new ArrayList();

list.add("1");

list.add("2");

list.add("3");

field1 = new FieldKey("0", "Item");

field2 = new FieldKey("1", "Size");

field3 = new FieldKey("2", "Quantity");

this.addFieldKey(field1);

this.addFieldKey(field2);

this.addFieldKey(field3);

setList(list);

setIncludeFields(true);

}

}
Back to top
goodvibrato



Joined: 13 Nov 2008
Posts: 8

PostPosted: Tue Nov 18, 2008 5:03 am    Post subject: Reply with quote

It works! Thanks for the help Don. The only thing I had to do was delete the al.clear() and al.addAll((List<MyObj>)me) lines in the createDefaultArrayList method and then make it return me instead of al, otherwise I wouldn't see any data. Anyway, if it wasn't for you I'd probably still be hunting for a tutorial on google. Thanks again.
Back to top
knugen



Joined: 28 Sep 2009
Posts: 16

PostPosted: Wed Apr 28, 2010 3:00 pm    Post subject: thanks Reply with quote

thank you so much, it works for me, too..
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo