Reputation: 56894
I have ran into 2 situations already that feel like they could be solved if JasperReports had some kind of JRDesignElementGroup
. I've checked the net.sf.jasperreports.engine.design.
package high and low but can't find anything like it, although I was able to find a JRDesignGroup
which looks like it sort of accomplishes what I'm looking for, but I'm not very sure of that.
Here are the siutations where my issue is cropping up:
(1) Grouping multiple text fields together:
I'd like to have a report where I can print out the names and values of a bunch of metrics; something that looks like:
Name: John Smith
Email: [email protected]
I was hoping to accomplish this by creating 1 JRDesignElement
subclass instance, and adding it to a band in my JasperDesign
object. However, after thinking it over, that setup requires 3 separate JRDesignElement
s: 2 JRDesignTextField
s (for the metric name and value) and 1 JRDesignStaticText
for the colon (":") and space between them.
Is there a way to append these 3 items to a group and then just add the group to a band?
(2) Group an image and its title/caption
I also would like to do the same as above, except using JRDesignImage
and JRDesignStaticText
elements, where the image is an image to be displayed on my report, and the static text will be a title or caption to be placed above the image.
Is there any way to append these 2 items to a group and then just add the group to a band?
If so, can anyone provide JRXML, or even more preferably, some Java examples for how to do this appending? And if not, what's the solution/work-around?
Thanks in advance!
Upvotes: 2
Views: 6968
Reputation: 22857
(1) Grouping multiple text fields together:
JRDesignTextField
element //Detail
band = new JRDesignBand();
band.setHeight(40);
textField = new JRDesignTextField();
textField.setX(0);
textField.setY(0);
textField.setWidth(200);
textField.setHeight(40);
textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
textField.setStyle(normalStyle);
textField.setMarkup(JRCommonText.MARKUP_HTML);
expression = new JRDesignExpression();
expression.setText("\"<b>Name: </b>\" + $F{Name} + \"<br/><b>Email: </b>\" + $F{Email}");
textField.setExpression(expression);
textField.getLineBox().getLeftPen().setLineWidth(1);
textField.getLineBox().getTopPen().setLineWidth(1);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().setLeftPadding(10);
band.addElement(textField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
JRDesignStaticText
and two JRDesignTextField
elements together //Detail
band = new JRDesignBand();
band.setHeight(40);
JRDesignStaticText staticText = new JRDesignStaticText();
staticText.setX(0);
staticText.setY(0);
staticText.setWidth(60);
staticText.setHeight(20);
staticText.setMode(ModeEnum.OPAQUE);
staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
staticText.setStyle(boldStyle);
staticText.setText("Name: ");
staticText.getLineBox().getLeftPen().setLineWidth(1);
staticText.getLineBox().getTopPen().setLineWidth(1);
staticText.getLineBox().setLeftPadding(10);
band.addElement(staticText);
textField = new JRDesignTextField();
textField.setX(60);
textField.setY(0);
textField.setWidth(200);
textField.setHeight(20);
textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
textField.setStyle(normalStyle);
expression = new JRDesignExpression();
expression.setText("$F{Name}");
textField.setExpression(expression);
textField.getLineBox().getTopPen().setLineWidth(1);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().setLeftPadding(10);
band.addElement(textField);
staticText = new JRDesignStaticText();
staticText.setX(0);
staticText.setY(20);
staticText.setWidth(60);
staticText.setHeight(20);
staticText.setMode(ModeEnum.OPAQUE);
staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
staticText.setStyle(boldStyle);
staticText.setText("Email: ");
staticText.getLineBox().getLeftPen().setLineWidth(1);
staticText.getLineBox().getBottomPen().setLineWidth(1);
staticText.getLineBox().setLeftPadding(10);
band.addElement(staticText);
textField = new JRDesignTextField();
textField.setStretchWithOverflow(true);
textField.setX(60);
textField.setY(20);
textField.setWidth(200);
textField.setHeight(20);
textField.setPositionType(PositionTypeEnum.FLOAT);
textField.setStyle(normalStyle);
expression = new JRDesignExpression();
expression.setText("$F{Email}");
textField.setExpression(expression);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().getBottomPen().setLineWidth(1);
textField.getLineBox().setLeftPadding(10);
band.addElement(textField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
(2) Group an image and its title/caption
Using three JRDesignImage
and single JRDesignStaticText
elements
band = new JRDesignBand();
band.setHeight(110);
String imgPath1 = "\"jasperreports.png\"";
String imgPath2 = "\"js_logo.png\"";
int img1Width = 105;
int img2Width = 200;
int distance = 20;
expression = new JRDesignExpression();
expression.setText(imgPath1);
JRDesignImage image = new JRDesignImage(jasperDesign);
image.setX(0);
image.setY(0);
image.setWidth(img1Width);
image.setHeight(26);
image.setScaleImage(ScaleImageEnum.FILL_FRAME);
image.setExpression(expression);
band.addElement(image);
expression = new JRDesignExpression();
expression.setText(imgPath2);
image = new JRDesignImage(jasperDesign);
image.setX(distance + img1Width);
image.setY(0);
image.setWidth(img2Width);
image.setHeight(87);
image.setScaleImage(ScaleImageEnum.FILL_FRAME);
image.setExpression(expression);
band.addElement(image);
expression = new JRDesignExpression();
expression.setText(imgPath1);
image = new JRDesignImage(jasperDesign);
image.setX(2*distance + img1Width + img2Width);
image.setY(0);
image.setWidth(img1Width);
image.setHeight(26);
image.setScaleImage(ScaleImageEnum.FILL_FRAME);
image.setExpression(expression);
band.addElement(image);
JRDesignStaticText staticText = new JRDesignStaticText();
staticText.setX(200);
staticText.setY(90);
staticText.setWidth(60);
staticText.setHeight(20);
staticText.setMode(ModeEnum.OPAQUE);
staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
staticText.setStyle(boldStyle);
staticText.setText("The title above images");
band.addElement(staticText);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
Upvotes: 1