Search code examples
javajasper-reportsdynamic-reports

How to get column under the title in DynamicReports?


enter image description here

I need to get the column right under the title and i don't need any gaps in between. And how to set width to the column? I have given setWidth(50) but its not getting and the border is covering entire right side of the page

Here is my code:

public class DynamicReport {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/marketing_database","root","root");
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        JasperReportBuilder report = DynamicReports.report();//a new report
        StyleBuilder plainStyle = stl.style().setFontName("FreeUniversal");

        StyleBuilder boldStyle = stl.style(plainStyle).bold().setBorder(stl.pen1Point());
        StyleBuilder col1style = stl.style().setBorder(stl.pen1Point());

        report
          .title(Components.text("Tax Invoice Cum Delivery Challan").setStyle(boldStyle)
          .setHorizontalAlignment(HorizontalAlignment.CENTER))
          .columns(col.column("", "companyName", type.stringType()).setWidth(50).setStyle(col1style))
          .pageFooter(Components.pageXofY())
          .setDataSource("SELECT companyName FROM marketing_database.company_profile", 
                                      connection);
        try {
            report.show();
            report.toPdf(new FileOutputStream("c:/report.pdf"));
        } catch (DRException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      }
}

Any help in this will be appreciated

UPDATED:

I got how to set the width for a column. I used this code:

 col.column("", "companyName", type.stringType()).setFixedWidth(20)

Solution

  • report
    
    
    
            .columnHeader(//title of the report
    
    
                  Components.text("Tax Invoice Cum Delivery Challan").setStyle(boldStyle).setFixedHeight(10)
                  .setHorizontalAlignment(HorizontalAlignment.CENTER))
    
                 .columns(
                         col.column("", "companyName", type.stringType()).setFixedWidth(200).setStyle(col1style))
    
                  .pageFooter(Components.pageXofY())//show page number on the page footer
                  .setDataSource("SELECT companyName FROM marketing_database.company_profile", 
                                          connection);
    
            try {
                        //show the report
                report.show();
    
                        //export the report to a pdf file
                report.toPdf(new FileOutputStream("c:/report.pdf"));
            } catch (DRException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
          }