TABLEMODEL AS DATASOURCE IN JASPER REPORTS

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

image

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 http://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 !!!

About these ads

19 thoughts on “TABLEMODEL AS DATASOURCE IN JASPER REPORTS

  1. 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!

  2. 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 ?

  3. 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

  4. 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

  5. Pingback: 2010 in review « Gilbert Adjin Tech Blog

  6. thanks for your guide..
    but why xputils doesnt recocnized in my class?
    it said create class or create field for ‘exputils’

  7. 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..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s