Adding image to a report dynamically- iReport

This blog is about adding an image to a report in iReport dynamically. In this example I'm going to do it through a netbeans plugin (using iReport platform).

First create a netbeans module. Select iReport as the platform. Add an action class to the project. In gui registration , I selected it to get a menuitem under the Format menu.

then add following code to Action class.


public void actionPerformed(ActionEvent e) {
boolean isImageAdded=false;
JasperDesign report=IReportManager.getInstance().getActiveReport();
if( report != null ){
JRParameter param[]=report.getParameters();
for(int i=0; i if(param[i].getName().equals("photo")){
JOptionPane.showMessageDialog(null, "The logo is already added to the report");
isImageAdded= true;
}
}
if(!isImageAdded ){
getImage();
}
}
}

private void getImage(){
Image photo;
JFileChooser fc = new JFileChooser();

if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
fc.setCurrentDirectory(fc.getCurrentDirectory());
ImageIcon icon = new ImageIcon(fc.getSelectedFile().getAbsolutePath());
icon = new ImageIcon(icon.getImage());
photo = icon.getImage();

JasperDesign report=IReportManager.getInstance().getActiveReport();

if (report != null){
try {
JRDesignParameter parameter =new JRDesignParameter();
parameter.setName("photo");
parameter.setValueClass(java.lang.Object.class);
report.addParameter(parameter);

IReportManager.getInstance().saveiReportConfiguration();

JRDesignExpression expression=new JRDesignExpression();
expression.setText("$P{photo}");
expression.setValueClass(java.awt.Image.class);

JRDesignImage image = new JRDesignImage(null);
image.setY(0);
image.setWidth(photo.getWidth(fc));
image.setHeight(photo.getHeight(fc));
// image.setScaleImage(JRImage.SCALE_IMAGE_CLIP);
image.setExpression(expression);

JRDesignBand band=(JRDesignBand) report.getTitle();
band.setHeight(photo.getHeight(fc));
band.addElement(image);

IReportManager.getInstance().saveiReportConfiguration();

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


Map param = new HashMap();
param.put("photo", photo);

try {
generateReports( report,param);
} catch (JRException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}


private void generateReports(JasperDesign report, Map param) throws JRException {

try {

if (IReportManager.getInstance().getActiveReport() != null){

JasperReport jasperReport = JasperCompileManager.compileReport(report);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());

JasperViewer.viewReport(jasperPrint, false);
}
}
catch (JRException ex)
{Exceptions.printStackTrace(ex);
}

}



Now run the project.

The jarfile rt.jar has no source attachment

This is for people who have faced that problem when using myeclipse. Well, I'm going to give you a tip.

"the jarfile rt.jar has no source attachment" is not part of the actual error. An exception is being thrown in your programme. Eclipse is trying to show you the source code of error, But it cant find the code of relevant classes (it is possible)
Eclipse is telling you it can't provide this help. This is a seperate matter
from the actual exception itself.

So , pay your attention to more important area without worring about this. I'm telling you this from my experience :)

more info :
http://www.velocityreviews.com/forums/t151773-need-help-the-jarfile-jsr173_api-jar-has-no-source-attachment.html