rainer198
rainer198

Reputation: 3263

In DynamicReports, how do you set absolute coordinates of report elements

In JasperReports, you always set absolute x and y coordinates for positioning report elements. For example, the following example renders a black rectangle (upper left corner at (0,0)) which contains a white text field "Hello World" which starts at x=70:

<rectangle>
    <reportElement x="0" y="0" width="557" height="20" backcolor="#000000" />
    <graphicElement fill="Solid">
        <pen lineWidth="0"/>
    </graphicElement>
</rectangle>

<textField>
    <reportElement x="70" y="2" width="100" height="14" style="pageLayoutFont"/>
    <textElement textAlignment="Left" verticalAlignment="Middle" />
    <textFieldExpression class="java.lang.String">
        <![CDATA["Hello World"]]>
    </textFieldExpression>
</textField>

Now, I want to achieve the same using DynamicReports. It should work similar to this:

report().addDetail(
    cmp.rectangle()
       .setDimension(557, 20).setStyle(...),
    cmp.text("Hello World").setStyle(...)   
);

However, I can't find a way to determine the positioning (in the Java example above, the text is rendered below the rectangle, not within.

Upvotes: 3

Views: 2759

Answers (1)

rainer198
rainer198

Reputation: 3263

For this particular case (a rectangle as surrounding box of a text field), it is possible to work with horizontalLists. The horizontal list has the rectangle as background (colour) and has a fixed dimension. For shifting the text to y=2 you could add a thin gap element (same width) as first horizontal list containment and then append a newRow. Then, a second gap with with=70 appended by the text field Hello World . Alternatively, you can ommit the gap and add a padding style to the text field.

cmp.horizontalList()
    .setFixedDimension(557, 20)
    .setBackgroundComponent(...)
    .add(
        //1. a thin gap element
        cmp.gap(557,2)
    )
    .newRow()
    .add
    (
        //2. a gap of width 70
        cmp.gap(70,13),
        //3. the text field
        cmp.text("Hello World").setStyle(...)
    )

Although this works for this case, it is kind of an indirect approach which easily can get confusing. Further, I wonder if there is a solution for every positioning task you can think of.

Upvotes: 1

Related Questions