syam houng syam houng - 4 years ago 168
Java Question

JasperReport using DataCollection

I am trying to create a report using

JRBeanCollectionDataSource
but I have a problem. I need to print two records using checkboxes in a
JTable
but only one record is shown in JasperReport.

Here is my code :

for (int i = 0; i < model.getRowCount(); i++) {
for(int j=0;j< model.getColumnCount();j++){
Boolean value = (Boolean) model.getValueAt(i, 11);// check state
if (value) {
System.out.println(model.getValueAt(i, j));// second column value
struk=new ArrayList<ListTrxPrint>();
ListTrxPrint trx=new ListTrxPrint();
trx.setId(model.getValueAt(i, 0).toString());
trx.setMsisdn(model.getValueAt(i, 4).toString());
trx.setExecute_date(model.getValueAt(i, 3).toString());
trx.setNominal(model.getValueAt(i, 6).toString());
trx.setSales_price(model.getValueAt(i,7).toString());
trx.setUser_name(model.getValueAt(i, 1).toString());
struk.add(trx);
bean=new JRBeanCollectionDataSource(struk);
}
}
}

try{
JasperDesign design=JRXmlLoader.load("C:\\Users\\PPS SAMSUL\\Documents\\RPTStruk.jrxml");
JasperReport jReport=JasperCompileManager.compileReport(design);
JasperPrint jPrint=JasperFillManager.fillReport(jReport, null,bean);
JasperViewer.viewReport(jPrint, false);
JasperViewer.getWindows();
}
catch(Exception e){
e.printStackTrace();
}


What's wrong with my code?

Answer Source

You need to move the ArrayList and JRBeanCollectionDataSource out of your loop, now you are recreating them each time you loop, furthermore I can not see any need to loop on all columns.

The code could be similar to:.

struk=new ArrayList<ListTrxPrint>();
for (int i = 0; i < model.getRowCount(); i++) {
   Boolean value = (Boolean) model.getValueAt(i, 11);// check state
   if (value) {
       ListTrxPrint trx=new ListTrxPrint();
       trx.setId(model.getValueAt(i, 0).toString());
       trx.setMsisdn(model.getValueAt(i, 4).toString());
       trx.setExecute_date(model.getValueAt(i, 3).toString());
       trx.setNominal(model.getValueAt(i, 6).toString());
       trx.setSales_price(model.getValueAt(i,7).toString());
       trx.setUser_name(model.getValueAt(i, 1).toString());
       struk.add(trx);
   }
 }      
 bean=new JRBeanCollectionDataSource(struk);  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download