0

I have a jtable which is dynamically getting data. For an example, i have a jtable which loads all the employee information by search button click. Then user can filter data from those employees. From departments, Employee numbers etc.. I want to pass those filtered data into ireports. Not by taking data from the database. Only by taking data from the jtable. i wrote following code. but it shows errors.

try {
                DefaultTableModel df = ( DefaultTableModel ) jTable1.getModel();
                JRTableModelDataSource dataSource = new JRTableModelDataSource(df);
                String reportSource = "./Leave.jrxml";
                JasperReport jr = JasperCompileManager.compileReport(reportSource);
                Map<String, object> params = new HashMap<String, object>();
                params.put("title1" , "title 1");
                JasperPrint jp = JasperFillManager.fillReport(jr, params, dataSource);
            } catch (JRException ex) {
                Logger.getLogger(LeaveManagementInfosystem.class.getName()).log(Level.SEVERE, null, ex);
            } 

it says "title 1" is not an object. I tried it again by not adding "". but same error is occurred. how can i get rid of this error.

Alex K
  • 22,315
  • 19
  • 108
  • 236
kate
  • 13
  • 1
  • 6

2 Answers2

2

We will assume that that you have a JTable with 3 columns which are s_id,name,age.

enter image description here

In your jasper report you should create fields with same names according to your table columns.

enter image description here

<field name="s_id" class="java.lang.String"/>
    <field name="name" class="java.lang.String"/>
    <field name="age" class="java.lang.String"/>

You dont need to pass parameters to your jasper report.

JasperReport jasperReport = JasperCompileManager.compileReport(
                    source);
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
                    null, new JRTableModelDataSource(model));
  JasperViewer.viewReport(jasperPrint, false);
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
0

create parameters with same as column names bind the value with their fields and add the params to query above java code works for me

alwin
  • 1
  • 1