Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList populated table doesn't save values.

 
Robert Verdes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Verdes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic