kate
kate

Reputation: 13

Passing data from a Jtable to Ireport

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.

Upvotes: 0

Views: 4299

Answers (2)

alwin
alwin

Reputation: 1

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

Upvotes: 0

Madushan Perera
Madushan Perera

Reputation: 2598

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);

Upvotes: 2

Related Questions