aspose file tools*
The moose likes JSF and the fly likes ArrayList populated table doesn't save values. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "ArrayList populated table doesn Watch "ArrayList populated table doesn New topic
Author

ArrayList populated table doesn't save values.

Robert Verdes
Greenhorn

Joined: Sep 02, 2009
Posts: 5
Hey everybody, i'm almost new to JSF, i've been doing it a while but i'm constantly stumbling into issues, like this one:

I have a table, populated with an ArrayList<myObject>. All the columns' types are TextFields, so the user can change the contents. The problem is it won't change a single thing. It remains the same as when populated.

Now i've tried this with an array (myObject[]) and it works. But i can't use it since i have to be able to add new rows to it.
I've also tried using an Object List Data Provider, but i can't manage to make it work. It won't accept any objectType value. i've tried java.util.ArrayList (which is the only one that works but no contents to choose from). java.util.ArrayList<my.package.myObject> is not accepted.

What can i do, except adding eventHandlers to every column object and forcing the change in the initial array list? (EDIT: and i'm not realy shure i can do that either without too much complication)

Thanks! oh, and PS: 1st post, nice to be here
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Welcome to the JavaRanch!

I'm not quite sure I understood all that. If you want to add columns to a dataTable, that can be a bit complicated. Adding rows is fairly easy.

First, make sure your backing bean is in the proper scope. Since request-scope objects are created and destroyed every time you talk to the server, usually the bean containing a datatable's data model should be in session scope.

You can define a 2-dimensional table as a List (rows) of Lists (columns in row). Or a List of Arrays. Or of Array of Lists. Or whatever collections suit you.

To make the model expandable in rows, an ArrayList is a good model object to use. An Array cannot add rows. Simply wrap the ArrayList object containing your data rows in your DataModel (ListDataModel). To add rows, you need something like an "Insert" button whose Action processor accesses the wrapped ArrayList and adds a new row. You can either use the getWrappedData method on the model to get access to the ArrayList, or do like I usually do and save the ArrayList as a class member. It's not as "safe" as simply pulling the wrapped data, but on the other hand, if you pull the wrapped data, you'd have to cast it, and that's not "safe" either. Pick your poison.

A very good illustration of all this and more is Rick Hightower's "JSF for Nonbelievers" series on IBM DeveloperWorks. It's easy to find if you do a web search on the article name.


Customer surveys are for companies who didn't pay proper attention to begin with.
Robert Verdes
Greenhorn

Joined: Sep 02, 2009
Posts: 5
Hey Tim, thanks for the reply.

Sorry i've been a little vague. I've taken your suggestion and read the JSF for Nonbelievers series (first two for now), and saw the example for the DataModel. I didnt know about it, as i'm using NetBeans' design view to mingle with my components, and it only has Providers.

What i was have here, is a table in a page, that has to contain 2 columns of data. My array / array list was in the SessionBean, formed of instances of type MyObject (which has these attributes: String attr1; and String attr2). The table used TextFields for displaying this data, so it can be changed easily.

What was happening was: when binding the table row set to the MyObject[] dataArray, what i was changing in the table's TextFields was also changing in the array.
But when i was binding it with ArrayList<MyObject> dataList, the changes didn't stick.

So i tried ObjectListDataProvider, but was unable to make it work (i still don't know how to make it work from within the Design View: its objectType attribute is set to null, and though i don't really know what it should be set to, i tried all combinations of java.util.ArrayList<my.package.myObject>, my.package.myObject, java.util.ArrayList, but didn't work.

So now after seeing the DataModel example on IBM DeveloperWorks, i managed to manually set up the ObjectListDataProvider. So thanks for the good feedback

If you know why i'm not able to make it happen automatically ( via rightClick on the table -> table Layout -> ... ), or other suggestions, i'd appreciate the feedback.

Best regards
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Robert Verdes wrote:Hey Tim, thanks for the reply.

Sorry i've been a little vague. I've taken your suggestion and read the JSF for Nonbelievers series (first two for now), and saw the example for the DataModel. I didnt know about it, as i'm using NetBeans' design view to mingle with my components, and it only has Providers.

What i was have here, is a table in a page, that has to contain 2 columns of data. My array / array list was in the SessionBean, formed of instances of type MyObject (which has these attributes: String attr1; and String attr2). The table used TextFields for displaying this data, so it can be changed easily.

What was happening was: when binding the table row set to the MyObject[] dataArray, what i was changing in the table's TextFields was also changing in the array.
But when i was binding it with ArrayList<MyObject> dataList, the changes didn't stick.

So i tried ObjectListDataProvider, but was unable to make it work (i still don't know how to make it work from within the Design View: its objectType attribute is set to null, and though i don't really know what it should be set to, i tried all combinations of java.util.ArrayList<my.package.myObject>, my.package.myObject, java.util.ArrayList, but didn't work.

So now after seeing the DataModel example on IBM DeveloperWorks, i managed to manually set up the ObjectListDataProvider. So thanks for the good feedback

If you know why i'm not able to make it happen automatically ( via rightClick on the table -> table Layout -> ... ), or other suggestions, i'd appreciate the feedback.

Best regards


I'm an Eclipse junkie myself, so the last time I used Netbeans, I think is was caused "Forte". But the problem with letting an IDE do work for you is that it's not as flexible as manual code and it doesn't promote understanding. So I usually don't use the IDE. Whatever time I'd save having that stuff done for me I often lose (with interest) fighting the IDE.
Jag Bains
Ranch Hand

Joined: Nov 19, 2002
Posts: 39
Hi,

I have the same issue. If you have a fix, can you paste a copy of your JSP and the relevant part of your backing bean and session bean so that I can see how you managed to get it to work.

Thanks a lot
 
Consider Paul's rocket mass heater.
 
subject: ArrayList populated table doesn't save values.