Benny
Benny

Reputation: 87

How to change cell width from List in JasperReports dynamically with Java API

I cant find the right method to change the cell width dynamically from a List in JasperReports.

final JasperDesign template = JRXmlLoader.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("report_tarif_comparison.jrxml"));
final JRBand[] bands = template.getAllBands();
final JRElement element = bands[1].getElementByKey("compareList");

I get the List by getElementByKey, but the element Object has no method such as setCellWidth or setCellHeight

Part of my jrxml file:

<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Horizontal" ignoreWidth="false">
    <datasetRun subDataset="compareTarif" uuid="03bdfbc4-19ae-47a1-a27e-bbacde25965f">
        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{policies})]]></dataSourceExpression>
    </datasetRun>
    <jr:listContents height="494" width="138">
        ....//textfields
    </jr:listContents>
</jr:list>

In Jaspersoft Studio, I can change the Cell Width in the properties window from the List:

Jasper Studio, Properties from List

Upvotes: 2

Views: 1209

Answers (1)

dada67
dada67

Reputation: 5093

You'll have to do something like this:

JRComponentElement element = (JRComponentElement) bands[1].getElementByKey("compareList");
ListComponent component = (ListComponent) element.getComponent();
DesignListContents contents = (DesignListContents) component.getContents();
contents.setWidth(...);

Note that this only change the width of the cell. If you also want to make the text fields inside wider, you'll have to do it explicitly; you can get the elements via contents.getElements().

Upvotes: 2

Related Questions