Wednesday, January 12, 2011

How to Integrating Jasper reports into SWT

This Rar Files contain all the jar files that To call and View the jasper report in SWT and swing application.....
jar files at..
http://jumbofiles.com/ud7livpynjfg

How To Integrating Jasper reports into SWT



And Exampel Code For SWT application...


Ireport LifeCycle.JPG


LedgerDetails.java Veiw Class Which call and display the .jrxml Files.....

package mcg.microfin.account.view;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import mcg.microfin.common.db.
DbConnection;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jasperassistant.designer.viewer.ViewerComposite;
import com.swtdesigner.SWTResourceManager;

public class LedgerDetails extends ViewPart {
private Button chkExpenses;
private Button chkIncome;
private Button chkLiabilities;
private Button chkAll;
private Button chkAssets;
private Text text;
private Text text_1;
private Composite comLedgerReport;

// Using the SWTJasperViewer to load the Jasper report to the SWT composite
private ViewerComposite viewerComposite;

/// viewerComposite is the viewer composite where report will display......

public LedgerDetails() {

}

@Override
public void createPartControl(Composite parent) {

// base container holding all other components
Composite comContainer = new Composite(parent, SWT.NONE);
comContainer.setFont(SWTResourceManager
.getFont("Segoe UI", 9, SWT.BOLD));

// Form title
Label lblGeneralLedgerDetails = new Label(comContainer, SWT.NONE);
lblGeneralLedgerDetails.setFont(SWTResourceManager.getFont("Tahoma",
10, SWT.BOLD));
lblGeneralLedgerDetails.setBounds(429, 10, 152, 21);
lblGeneralLedgerDetails.setText("General Ledger Details");

// composite for holding the choices for generating report
Composite comChoices = new Composite(comContainer, SWT.NONE);
comChoices.setBounds(10, 10, 190, 709);

// group holding the account type selection
Group grpAccTypeSelection = new Group(comChoices, SWT.NONE);
grpAccTypeSelection.setText("Account Type");
grpAccTypeSelection.setBounds(10, 20, 170, 160);

// checkbox for assets account type
chkAssets = new Button(grpAccTypeSelection, SWT.CHECK);
chkAssets.setBounds(10, 27, 93, 16);
chkAssets.setText("Assets");

// checkbox for expenses account type
chkExpenses = new Button(grpAccTypeSelection, SWT.CHECK);
chkExpenses.setBounds(10, 49, 93, 16);
chkExpenses.setText("Expenses");

// checkbox for income account type
chkIncome = new Button(grpAccTypeSelection, SWT.CHECK);
chkIncome.setBounds(10, 70, 93, 16);
chkIncome.setText("Income");

// checkbox for liabilities account type
chkLiabilities = new Button(grpAccTypeSelection, SWT.CHECK);
chkLiabilities.setBounds(10, 92, 93, 16);
chkLiabilities.setText("Liabilities");

// checkbox for selecting all four account types
chkAll = new Button(grpAccTypeSelection, SWT.CHECK);
chkAll.setBounds(10, 114, 93, 16);
chkAll.setText("All");

// lblFrom label
Label lblFrom = new Label(comChoices, SWT.NONE);
lblFrom.setBounds(10, 209, 55, 15);
lblFrom.setText("From");

// From date
text = new Text(comChoices, SWT.BORDER);
text.setBounds(10, 228, 170, 21);

// lblTo label
Label lblTo = new Label(comChoices, SWT.NONE);
lblTo.setBounds(10, 271, 55, 15);
lblTo.setText("To");

// To date
text_1 = new Text(comChoices, SWT.BORDER);
text_1.setBounds(10, 292, 170, 21);

// label for Account Code
Label lblAccCode = new Label(comChoices, SWT.NONE);
lblAccCode.setBounds(10, 340, 91, 15);
lblAccCode.setText("Account Code");

// combo displaying the list of Account Codes
Combo combo = new Combo(comChoices, SWT.NONE);
combo.setBounds(10, 361, 170, 23);

// label for Account Head
Label lblAccHead = new Label(comChoices, SWT.NONE);
lblAccHead.setBounds(10, 399, 91, 15);
lblAccHead.setText("Account Head");

// composite for holding the ledger table(report)
comLedgerReport = new Composite(comContainer, SWT.NONE);
comLedgerReport.setBounds(206, 39, 802, 648);

// combo displaying the list of Account Heads
Combo combo_1 = new Combo(comChoices, SWT.NONE);
combo_1.setBounds(10, 420, 170, 23);

Button cmdView = new Button(comChoices, SWT.NONE);
cmdView.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {

/*
* Calling the generateReport() and passing the acc_code as
* parameter.
*/
JasperPrint jprint = null;
jprint = generateReport(text_1.getText());
// requesting jspser report ... it will return jasper print which is going to display under viewerComposite
jprint.setPageHeight(500);
jprint.setPageWidth(730);

try {

// Loading the JasperPrint object in the viewerComposite
viewerComposite.getReportViewer().setDocument(jprint);
viewerComposite.pack();
viewerComposite.setEnabled(true);

} catch (Exception e2) {
e2.printStackTrace();
}
}
});
cmdView.setBounds(10, 449, 75, 25);
cmdView.setText("View");

// Defining an instance of ViewerComposite into the view
viewerComposite = new ViewerComposite(comLedgerReport, SWT.BORDER);
viewerComposite.setBounds(10, 0, 767, 638);
viewerComposite.setLayoutData(new GridData(GridData.FILL,
GridData.FILL, true, true));

}

/**
* generateReport() that receives the parameters to generate the report,
* genereates the final JPrint ready to be outputted and returns the same.
*
* @param accCode
* ()
* @return JasperPrint (An instance of type JasperPrint that is ready to be
* outputted into the screen.)
*/
public static JasperPrint generateReport(String accCode) {
Connection conn = null;
JasperPrint myJPrint = null;

try {

conn = DbConnection.getConnection();

// Loading my jasper file
JasperDesign jasperDesign = null;
JasperReport jasperReport = null;

jasperDesign = JasperManager
.loadXmlDesign(LedgerDetails.class.getClassLoader()
.getResourceAsStream("ledgerReport.jrxml"));

// Incase of directly loading the jasper file.
// jasperReport = (JasperReport) JRLoader
// .loadObject(LedgerDetails.class.getClassLoader()
// .getResourceAsStream("report1.jasper"));

jasperReport = JasperCompileManager.compileReport(jasperDesign);

// Passing parameters to the report
Map<String, Object> parameters = new HashMap();
parameters.put("acc_code", accCode);

/*
* Filling the report with data from the database based on the
* parameters passed.
*/
myJPrint = JasperFillManager.fillReport(jasperReport, parameters,
conn);
parameters.clear();

} catch (JRException ex) {
ex.printStackTrace();
}

return myJPrint;

}

@Override
public void setFocus() {

}

}

///ledgerReport.jrxml is inside in lib/testAccount.jar files.....
--------___________------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Microfin
Bundle-SymbolicName: microfin;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: mcg.microfin.Activator
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ClassPath: .,
lib/sqljdbc4.jar,
lib/images.jar,
swt-grouplayout.jar,
lib/commons-beanutils-1.8.2.jar,
lib/commons-collections-3.2.1.jar,
lib/commons-dbcp-1.2.2.jar,
lib/commons-digester-1.7.jar,
lib/commons-javaflow-20060411.jar,
lib/commons-logging-1.1.jar,
lib/groovy-all-1.7.5.jar,
lib/iText-2.1.7.jar,
lib/iTextAsian.jar,
lib/jasperreports-3.7.6.jar,
lib/swtjasperviewer-1.2.0.jar,
lib/testAccount.jar



----------------------- This lib/testAccount.jar is the Report jar files which Contain jrxml and jspser files or each report..
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


build.properties



source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.,\
lib/images.jar,\
lib/sqljdbc4.jar,\
swt-grouplayout.jar,\
lib/commons-beanutils-1.8.2.jar,\
lib/commons-collections-3.2.1.jar,\
lib/commons-dbcp-1.2.2.jar,\
lib/commons-digester-1.7.jar,\
lib/commons-javaflow-20060411.jar,\
lib/commons-logging-1.1.jar,\
lib/groovy-all-1.7.5.jar,\
lib/iText-2.1.7.jar,\
lib/iTextAsian.jar,\
lib/jasperreports-3.7.6.jar,\
lib/swtjasperviewer-1.2.0.jar,\
lib/testAccount.jar


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Final out Put will look like this if all the procedure is goes correctly....


Final Output_jasperReport Clling IN SWT application..JPG



For More Information Use This link..........
http://www.eclipsezone.com/eclipse/forums/t53387.html

Again
If u have any problem then email on laxmangautam@gmail.com


1 comment: