Using the Object Array Data Provider

A forum poster recently asked for the following information:

I have a table with 5 columns, (week_no, objective, date1, date2 and add report). How do I get the date for date1( example, today's date)? How do I set the week number as auto increment by 1( example, row 1, the week_no is 1, row 2, the week_no is 2 and so on)? Lastly, how to I set the add report button to link to another JavaServer Pages file?

This scenario gives me an excellent opportunity to show how to use an Object Array Data Provider (OADP). I usually use the Object List Data Provider (OLDP), but OLDPs take a bit more work. (See the Using Hibernate tutorial for an OLDP example.) If you have a simple array of data, the OADP might be the better choice, as in this case. The following steps create a table similar to the one requested in the forum.

  1. Create a web application named ArrayTableExample.

    (If using the Visual Web Pack, be sure to create a Visual Web Application.)

  2. Create a JavaBeans object for the array. In this example, I chose to put the week number, start date, and end date in the bean. In the Projects window, right-click Source Packages > arraytableexample, choose New > Java Class, name the class WeekBean, and click Finish.
  3. Replace the class definition with the following code shown in bold.
    package arraytableexample;
    public class WeekBean 
            extends Object implements Serializable {
        private int weekNumber;
        private Calendar startDate;
        private Calendar endDate;
        private int dayOfWeek; 
        public WeekBean(int weekNumber) {
            this.weekNumber = weekNumber;        
            Calendar workingDate = Calendar.getInstance();
            workingDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
            int offset = weekNumber - 1; 
                + (offset \* 7));
            dayOfWeek = workingDate.get(Calendar.DAY_OF_WEEK);
            startDate = (Calendar) workingDate.clone();   
            Calendar.DAY_OF_YEAR, - (dayOfWeek - Calendar.SUNDAY));
            endDate = (Calendar) workingDate.clone();
                + (Calendar.SATURDAY - dayOfWeek));
        public WeekBean() {
        public int getWeekNumber() {
            return this.weekNumber;
        public void setWeekNumber(int weekNumber) {
            this.weekNumber = weekNumber;
        public Calendar getStartDate() {
            return this.startDate;
        public void setStartDate(Calendar startDate) {
            this.startDate = startDate;
        public Calendar getEndDate() {
            return endDate;
        public void setEndDate(Calendar endDate) {
            this.endDate = endDate;
  4. Right-click in the code and choose Fix Imports. Make sure the Fully Qualified Name for Calendar is java.util.Calendar, and click OK.

  5. Close and save the file.

  6. In this example, we want the table to show the same data throughout the session, so put the array property in the session bean. In the Projects window, double-click the Session Bean node to open in the source editor.

  7. Add the following code to the bottom of the class defintion, just before the ending brace. This code also adds properties for passing the selected start and end dates to other pages.
         \* Holds value of property weeks.
            WeekBean[] weeks = {
                new WeekBean(1),
                new WeekBean(2),
                new WeekBean(3),
                new WeekBean(4)
         \* Getter for property weeks.
         \* @return Value of property weeks.
        public WeekBean[] getWeeks() {
            return this.weeks;
         \* Holds value of property reportStartDate.
        private Calendar reportStartDate;
         \* Getter for property reportStartDate.
         \* @return Value of property reportStartDate.
        public Calendar getReportStartDate() {
            return this.reportStartDate;
         \* Setter for property reportStartDate.
         \* @param reportStartDate New value of property reportStartDate.
        public void setReportStartDate(Calendar reportStartDate) {
            this.reportStartDate = reportStartDate;
         \* Holds value of property reportEndDate.
        private Calendar reportEndDate;
         \* Getter for property reportEndDate.
         \* @return Value of property reportEndDate.
        public Calendar getReportEndDate() {
            return this.reportEndDate;
         \* Setter for property reportEndDate.
         \* @param reportEndDate New value of property reportEndDate.
        public void setReportEndDate(Calendar reportEndDate) {
            this.reportEndDate = reportEndDate;
  8. Right-click in the code and choose Fix Imports. Make sure the Fully Qualified Name for Calendar is java.util.Calendar, and click OK.

  9. Close and save

  10. View Page1 in the Visual Designer.

  11. In the Palette, expand the Data Providers section.

  12. Drag the Object Array Data Provider node and drop it on Page1.

  13. If the properties for the OADP do not appear in the Properties window, select Page1 > objectArrayDataProvider1 in the Outline window to make its properties appear.

  14. In the Properties window, change the id to weekDataProvider.

  15. Here is where it starts getting kludgy. You should be able to choose weeks from the array drop-down list, but it propably is not appearing at this point. If weeks is not in the drop-down list, choose Clean and Build Main Project from the main menu. After the build completes, close and reopen the project.

  16. In the Outline window, select Page1 > weekDataProvider.

  17. In the Properties window, select weeks (SessionBean1) from the array drop-down list.

  18. Drag a Table component from the Palette and drop it on Page1.

  19. Right-click the Table component and choose Table Layout.

  20. In the Table Layout dialog box, choose weekDataProvider from the Get Table From drop-down list.

  21. Use the Up and Down buttons to put the values in the Selected list in the following order:
  22. (Optional) Set the Header Text values for the columns to Week, Start Date, and End Date.

  23. Click New to add another column.

  24. In the new column, set the Header Text to blank, select Button from the Component Type drop-down list, and set the Value Expression to Show Me.

  25. Click OK to dismiss the dialog box.

    Again, it gets kludgy. Only one row appears, which says "No items found", and there is no button. Don't worry, it will work out in the end.

  26. In the Outline window, expand table1 > tableRowGroup1 > tableColumn4, right-click button1, and choose Edit Action Event Handler. opens in the source editor and scrolls to the newly added button1_action method.

  27. Add the code shown in bold to the method.
      public String button1_action() {
            return null;
  28. Right-click in the code and choose Fix Imports. Make sure the Fully Qualified Name for Calendar is java.util.Calendar, and click OK.

  29. Typically, you would set up page navigation to go to another page to display the report. For simplicity, click Design and drop a Static Text component on the page to display the selected start date.

  30. Right-click the Static Text component and choose Bind to Data.

  31. Click Bind to an Object, select SessionBean1 > reportStartDate, and click OK.

  32. Click Run Main Project. When the page appears, click the button in each row to verify that the action method returns the right value.

Winston has many blog entries about the dataprovider. A good place to start is his blog titled What is this Data Provider in Sun Java Studio Creator anyway?. Another good resource is Joel Brown's Weblog.


Thank You Very much. It would be nice if You take to the table textFields and dropDown lists too.

Posted by Ariel on March 06, 2007 at 02:17 AM PST #

See and the VWP equivalent at

Posted by diva#1 on March 06, 2007 at 07:04 AM PST #

I follow the steps above. But the problem is there's nothing appear in array drop-down list. Why's that? and how can i solve the problem?

Posted by wowerng on January 01, 2008 at 01:07 PM PST #


Are you using Sun Java Studio Creator IDE or NetBeans IDE. If you are using the NetBeans IDE, what is the version number.

Try doing a clean and build, closing the project, restarting the IDE and opening the project.

Posted by Diva #2 on January 03, 2008 at 05:12 AM PST #

I'm using NetBeans 5.5.1 . I followed your suggestion and it does work! thank you.

Posted by wowerng on January 03, 2008 at 10:32 AM PST #

Post a Comment:
Comments are closed for this entry.



« July 2016