## Using formulas to control object properties

One of the biggest features that formulas have is that you can use formulas to control the property values of objects at runtime. This provides you much more control, allowing you to customize your report and have flexibility to control the way objects are displayed based on the data values.

When you edit the property values of an object in the Report Inspector, you may notice that if you click some properties, there will be a button appearing on the right of the drop-down arrow, and when you click this button, it will change to be another button and also the section will become empty. Then when you click the drop-down arrow, a drop-down menu will be listed (see illustration). If you select one item in the list, the property will be controlled according to the return value of the formula. This is called conditional formatting in JReport.

Notes:

- Usually when you open the drop-down list of this kind of property, there is no formula to be selected directly unless you have created some in the catalog before building the report. You can also click the <Create Formula> item to create one as required.
- When you are composing a new formula that directly binds to a property using the Report Inspector, make sure the data type of return value is the same as the property type. Otherwise, the new formula will not be bound to this property, although it will be created in the catalog data source.

### Example: Changing the banded panel background color at runtime

This example uses the formula MColor to control the Background property of the detail panel of a banded report at runtime. You can find this formula in the catalog SampleReports. The following is the content of the formula:

    String sc = "" + (@MColor_RecordNumber *10 );
    Number idx = InStr(".",sc);
    if(idx != -1)
    sc = Left(sc, idx);
    if(Length(sc) < 2)
    sc = "0" + sc;
    String s = "0x00" + sc + "7f";
    return s

In this example we will vary the middle hexadecimal digits of the RGB color values. The Red value is fixed at 0x00, the Blue value is fixed at 0x7f. The Green value will change according to the number of records displayed. Record 1 will use color 0x00107f, Record 2 will use 0x00207f, and so on.

Take the following steps:

1. Create a standard banded report in JReport Designer.
2. In the design area, select the detail panel (DP) of the banded object.
3. In the Properties panel of the Report Inspector, click the value cell of property Background, and click . From the drop-down list, select MColor.
4. Click the View tab to view the report. The report will be shown somewhat as below, depending on the report you use.