Search code examples
jasper-reports

Dynamically show or hide table in jasper report


I am facing problem to show or hide the table in Jasper Report.

When no data are available then it's show a horizontal line.

I want to hide the static text field and table from report when the query retrieves no data.

Can anyone help me?

Here I have attached my output.

enter image description here


Solution

  • You could try setting a Print When expression for the static text and also for each column of the table.

    Here's an example of how it could look:

    // determine somewhere whether the received table data is empty or not.
    // save the result as a boolean and add it to report parameters
    boolean ifTableEmpty = false;  // for example
    parameters.put("ifTableEmpty", ifTableEmpty);
    
    // create a Print When expression
    JRDesignExpression whenToPrintTheTable = new JRDesignExpression();
    whenToPrintTheTable.setText("$P{ifTableEmpty}");    
    
    // add the expression to your static text and similarly for each column
    staticText.setPrintWhenExpression(whenToPrintTheTable);