I'm currently working on building reports using DynamicReports for my project.
I had a lot of problems doing this since it is my first time using such feature in java.
What I am trying to do is to create Columns dynamically. I have already solved the part of DynamicDataSource but now I'm stuck on columns.
here is my code:
public void raports(){
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int mColumns = model.getColumnCount();
int mRows = model.getRowCount();
String[] arrColumns = new String[mColumns];
for(int i=0; i<mColumns; i++){
arrColumns[i] = model.getColumnName(i);
}
DRDataSource dataSource = new DRDataSource(arrColumns);
for (int i = 0; i < mRows; i++) {
for (int j = 0; j<mColumns; j++){
dataSource.add(model.getValueAt(i, j).toString());
}
}
JasperReportBuilder report = DynamicReports.report();//a new report
StyleBuilder boldStyle = stl.style().bold();
StyleBuilder titleStyle = stl.style(boldCenteredStyle)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setFontSize(15);
StyleBuilder boldCenteredStyle = stl.style(boldStyle)
.setHorizontalAlignment(HorizontalAlignment.CENTER);
StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle)
.setBorder(stl.pen1Point())
.setBackgroundColor(Color.LIGHT_GRAY);
report
.setColumnTitleStyle(columnTitleStyle)
.highlightDetailEvenRows()
.columns(
// Columns.column("Patient ID", "Patient_ID", DataTypes.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT),
// Columns.column("First Name", "First_Name", DataTypes.stringType()),
// Columns.column("Middle Name", "Middle_Name", DataTypes.stringType()),
// Columns.column("Last Name", "Last_Name", DataTypes.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT)
)
.title(
cmp.horizontalList()
.add( //cut the code here since this is the only part needed.
Referrences: Dynamic Reports Documentation
You could interrupt the call chains for report and use the following method in a for loop:
I hope it helps.