Jump to content
We've recently updated our Privacy Statement, available here ×

Generate a table in a report from a java beans field which is an array


comco

Recommended Posts

 Here's my scenario:

I'm generating reports from java beans and I have bean data like this:

class Person {

public String getName();

public String getAge();

public Phone[] getPhones();

}

class Phone {

public String getCountrySuffix();

public String getRegionCode();

public String getPrefix();

}

 

I'm using a beans datasource for the designer as this:

class Person_DataFactory {

public static Person[] generateBeans() {

Phone p1 = new Phone("p1", "q1", "r1");

Phone p2 = new Phone("p1", "q1", "r1");

Phone p3 = new Phone("p1", "q1", "r1");

Person p = new Person("name", "29", new Phone[] { p1, p2, p3 });

return new Person[] { p };

}

}

 

Now, I'm able to get the Person fields on the report. But I want for each person in the report a table containing its phones.

How can this be done?

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

 Since no one answered, after a quick brainstorm, here's the answer:

 

JasperReports expects a JRDataSource as a source for the table, passing it an Phone[] doesn't work:

$F{phones}

We have to create some kind of data source in the "Connection/Datasource exp".

For example, we use:

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList($F{phones}))

and it works like a charm!

Link to comment
Share on other sites

  • 4 years later...

Hello,

 

Not sure of how to comment on your answer so I guess I will re-answered again (I did vote for the original answer).

Ok, so I am trying to achieve the same that you did in your report. Let's say, I have a category and I want to added sub-categories. In my case I am able to print some information but not the information per each category. How do you actually pass both the Person and Phone information into the table? And later how you organize that information so it shows all the Phones  information under a Person, in the table? That is sort of a new topic of how you display/arrange that information, but I would like to know about that too Would you give me more details on that?

Thanks in advance

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...