It is a common requirement in many J2SE (client,swing) applications to present data in a tabular way and also print this tabular format as a report.
Jasper reports provides implementation that makes the task of generating reports from tabular formats simple in Swing
applications. In this demonstrations, we will be using Jasper reports 3.6.1, Netbeans 6.1 and Ireport 3.6.1.
Let start by designing our report. In generating reports from tablemodels the report fields must match the column names of the tablemodel, but sometimes it becomes impractical to use the actual column names as report fields. Jasper Reports provides a way to generate reports from TableModels without having to map the actual table columns to the report fields. We can name our
report fields COLUMN_X, where x is the column index, starting with zero. Note “COLUMN” all characters should be capitals as “column” will give you error message at run time.
Also in case you have 3 columns in your table and you define a report field “COLUMN_4” , which is for column 5, null values will be displayed for each row in the report under that field. To prevent null values from displaying in a text field you can edit the expression for that field e.g. (($F{COLUMN_4}==null)? "":$F{COLUMN_4}.toString()), which means if the if the value of $F{COLUMN_4} is null ,display nothing else display the value. To do this, right click the text field and select “edit expression”, an expression editor will pop up for you to change the expression.
Below is a picture of our report at design time
After creating our report, we go to our application (using Netbeans as the IDE), we add a JTable and a JButton to our JFrame. We the create a method which accepts the table model to create the report, tbProducts is the name of the Jtable. Check https://gilbertadjin.wordpress.com/2009/05/05/populating-a-jtable-with-a-collection-list/ to see how to populate a list of javabean object to a Jtable
private void generateReports(String name, Map param) {
try {
String source = "C:/sabonay/jasperreports/" + name + ".jrxml";
if (new File(source).exists() == false) {
xputils.showMessage("Please go to setting and Choose report Source");
return;
}
JasperReport jasperReport = JasperCompileManager.compileReport(source);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JRTableModelDataSource(tbProducts.getModel()));
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
e.printStackTrace();
System.out.println("reports Error " + e.toString());
}
Cheers !!!
thenks for lesson but param=?
i’m waiting for you pleas
and i did’n inderstand :
if (new File(source).exists() == false) {
xputils.showMessage(“Please go to setting and Choose report Source”);
return;
is to check if the report file (jasper report file) specified exist.
Map param = new HashMap();
Thanks for the tutorial first!
The report displayed, but it did not show any data in the detail section.
The report was 5 or 11 pages long, meaning it can know how many rows are their in the table, just the data is not showing.
And I could compile the .jasper file, the error is Field Not found: $F{Column_0}
Please hlep, thanks!
do send me the report file you have designed and the structure of the table you want to use as the datasource. gilclefes@gmail.com is my mail address
Many thanks for the tutorial. It helped me a lot specially when I needed some dynamic reports. But I’m facing a little problem, Not all the rows are shown on the report, and all the fields in the row printed on the report are the same, I mean they are all dates containing the same value!
Could you please help me ?
Send me a copy of ur jasper report file (jrxml) to have a look. (my mail address is gilclefes@gmail.com)
Hi Gilbert
thank you for your posts – I’m new to JR and could definitely learn from your explanations!
Cheers
Marko
Hello Gilbert
I still have one more question. I can see my table (which is in a subreport) with all the data now.
The only thing which doesn’t seem to work are the column headers – they don’t seem to be populated.
Could you please tell me what I need to write into my jrxml in order to populate the column titles as well (I don’t use a static text since my app is multilanguage compatible)?
Appreciate your help,
Cheers
Marko
Hello Gilbert,
Thanks for the nice post.
I have a question for you. I have a string in the table which is too long to fit in the given column width? Is it possible using JasperReport to somehow get the whole string in the report?
Right now it truncates the string, and does not give the whole string in Report.
For example,
I have a column called Description:
if some value is say “LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG DEscription”
than it would just show me “LOOOOOOOOO”
Please let me know if you know some work around.
Thanks,
Sakshar
Make sure the “Stretch with Overflow” property of the field is checked.
Thank you Gilbert… Exactly what I wanted.
[…] TABLEMODEL AS DATASOURCE IN JASPER REPORTS November 200913 comments […]
thanks for your guide..
but why xputils doesnt recocnized in my class?
it said create class or create field for ‘exputils’
ok.. xputils is a class i created. to show messages you can use “JOptionPane.showMessageDialog”
hi gilbert..
It work well, when i change with JOptionpane but when i run the method it wont show anyway..may i email my .jrxml file to u??
I guess there is something wrong with my jrxml
thanx a lot..
Your guide really help me..
you can send it to gilclefes@gmail.com
hii Gilbert..
i’ve sent it to your mail..
thanx a lot…