Index

Using Styles in Your Report

Introduction

JReport style is one of the most important concepts in both JReport Designer and JReport Enterprise Server. JReport styles, organized in style groups, can hold a large amount of pre-defined object properties, which gives you more controls over the appearance and presentation of your report. By using JReport styles, you can create various visual presentation sets from a single report, and change the visual presentation of your report at run-time according to your needs. 

What is a style?

A style in JReport is a set of object attributes that you can assign to objects in your report to alter their appearance and characteristics. When you apply a style, you apply a whole group of property entries at once. For example, you may want to format the headings of a report to make them stand out. Instead of formatting them step by step as 12pt, Arial, and center-aligned with blue background, you can simply achieve the same goal in one step by applying a pre-defined style that contains the style properties you desire.

Each style has a style type to indicate its application scope, e.g., a style with Label type can only be applied to label objects, and a style with DBField type can only be applied to a dbfield.

What is a style group?

A style group is a set of styles that you can apply to a report to quickly change its appearance and characteristics. When you apply a style group, you apply a whole group of styles at once. To apply a style group to several report objects, you must first have the report objects selected, and then apply the style group. The style of each style type will be applied to the report object of the corresponding type.

By simply selecting a style group at run time, you change the look and feel of your report in a dramatic way. JReport style groups can be nested, where the parent group is used as a container of other groups.

Static Styles and Dynamic Styles

JReport style falls into two categories - static style and dynamic style.

A static style is a style that you can directly apply to the object in report, which means that you can see the style effect immediately, just like what you get after setting the properties step by step in Report Inspector. The static styles are mainly used to simplify the repetitive formatting work during design period. The styles you apply using the Style box on the Standard toolbar are all static styles.

Starting with version 5.2 JReport supports a new feature - dynamic style groups. Both of the static and dynamic styles work together, and your existing reports still work as before.

A dynamic style, created and edited using Catalog Browser, is a style that takes effect at run-time; it doesn't affect the report design layout in design mode. You can switch among the style groups according to your needs at run-time to present your report in rather different look and feel. The styles you specify in the Style property entry in Report Inspector will run as dynamic styles.

Predefined Style Groups

There are some pre-defined style groups in JReport Designer, which is mainly used as a demonstration for reports that contain table objects and crosstabs.

In each predefined group, you can find styles named as column1 and column2. These two styles are used as an example to show how to alter the styles for each column of the table object. For reports that contain a table object, if you want the style of each table column to alter, in the Report Inspector, select the table object, specify a style group for the Pattern property entry. When you view the report, you will find the styles column1 and column2 are repeatedly applied to each table column, in order. If you choose to run the report using other style group, the style of the table columns will also be changed, so long as the style group you select contains the styles column1 and column2.

If you want to apply another different style for more table columns, create a new column type style using the name column3. The styles will be applied to the columns accordingly like below:

Suppose there are three column type style in the style group, which includes column1, column2, and column3.

Column Type Style Applies to Column of the Table object
column1 => the 1st table column
column2 => the 2nd table column
column3 => the 3rd table column
column1 => the 4th table column
... ... ...

You can create as many column type styles as you wish, please remember to name the style as column1, column2, column3, and so on.

Setting up Styles in JReport Designer

You create and set up your own styles in JReport Designer, using the Styles tab on Catalog Browser. To open Catalog Browser, on the View menu, click Catalog Browser.

To set up the dynamic style group feature for your report, take the following procedures:

  1. Create new dynamic style groups
  2. Create new dynamic styles
  3. Specify a dynamic style group for your report
  4. Link the dynamic styles to report objects

Create a new dynamic style group

To create a new dynamic style group,

  1. On the Styles tab of Catalog Browser, select the group node where you want to create the new group, and then click New Style Group on the toolbar.
  2. Specify the Group Name and Description, and then click OK.

Note:

Create a new dynamic style

  1. On the Styles tab of Catalog Browser, right-click the group node where you want to create the new style, and then click New Style.
  2. Specify the Style Name, Style Types and Description, and then click OK.
  3. Add the property items you want to change for the style, and then click Save. You can also change the property values on this dialog.
  4. On the Catalog Browser, double-click the style you created to show the Properties sheet.
  5. Edit the property entries for the style.
Notes:

Apply a static style

 

To apply a static style, first select one or more report objects to which you want apply the style, and then select a style or style group in the Style box on the Standard toolbar.

To apply a style that you created in Catalog Browser as a static style,

  1. In Report Inspector, click the report root node, scroll down the Properties sheet to find the StyleGroup property entry, and then select the style group that contains the style you want to apply from the StyleGroup drop-down list.
  2. On the report design layout, do one of the following:

    To apply a style to a single object, select the object, and then select the style in the Style box on the Standard toolbar.

    To apply styles to more than one object, select the objects, and then select the style group in the Style box on the Standard toolbar.

Notes:

Specify a dynamic style group for your report

By default, JReport Designer does not use any style group for the reports you developed. To switch among style group at run time, you must specify a default style group for your report.

In the Report Inspector, select a style group for the StyleGroup property.

By setting the StyleGroup property to a style group you switch on the dynamic style group feature for this report.

Link a dynamic style

You need to link a dynamic style with the report objects before they can take effect at run-time.

To link a dynamic style, first select one object, or more with the same type, to which you want to link the style, and then type the style name in the Style property entry in Report Inspector.

Notes:

Modify a style

To modify a style, do one of the following:

Note:

Sometimes you may not be able to edit the property entries in the Properties sheet. In such case, on the File menu, click Options, click the Advanced tab, and then uncheck the Forbid editing data object properties option.

Remove a style

On the Styles tab of Catalog Browser, right-click the style you want to remove, and then click Delete.

Modifying the XML Style Files

In addition to modifying the styles (style groups) in JReport Designer, you can modify the xml format styles (style groups) outside JReport Designer.

When you save the catalog, the style information will be saved to disk at the same time, the location of the related file is like below:

  File Name File Description Location
1. CatalogName_stl.xml Style Group Structure in the Catalog Catalog Folder (The directory where the catalog resides)
2. StyleGroupName_stl.xsd Group/Style information %JReportInstallRoot%\template

The CatalogName_stl.xml file keeps the style group structure information, which might be like the following sample.

The style group structure in Catalog Browser: The corresponding information in Tutorial_stl.xml:


<?xml version="1.0" encoding="UTF-8"?>
<StyleGroup>
<StyleGroup name="Default"></StyleGroup>
<StyleGroup name="GroupA"></StyleGroup>
<StyleGroup name="GroupB"></StyleGroup>
</StyleGroup>

The StyleGroupName_stl.xsd file keeps the group/style information, which in the above sample might be like the following:

The file content of GroupA_stl.xsd:



As you can see in the above code sample, an xml style group file contains style group information, style information, and the style property entry information. You can edit the attributes of an element, such as name, type, default, and so on.

To add a style entry, copy and paste the style information section, and then modify the attributes.

To add a style property entry, copy and paste any element line, and then modify the attributes.

To delete a style entry or style property entry, select corresponding section, and delete it.

To add a new style group,

  1. Copy and paste an existing xml style group file.
  2. Rename it using the expected name (Format: GroupName_stl.xsd).
  3. Change default attribute in the GroupName element line using the expected group name, please make sure it is the same as the group name specified in the xml style group file name.
  4. Add a new line for this group in the CatalogName_stl.xml file. For example, if the new group name is GroupC, then insert the following line to the file:

    <StyleGroup name="GroupC"></StyleGroup>
  5. Do the modification work in the xml style group file (GroupName_stl.xsd).
  6. Open the catalog in JReport Designer, you can see the changes.

Note:

Deploying Reports to Enterprise Server

When you deploy your reports to Enterprise Server and you have set Style Group property, Deploy Wizard will ask you to transfer the style group files.

Add the style groups and then click OK, the files get transferred.

Viewing Reports on JReport Enterprise Server

After you set up the styles for your reports in JReport Designer, you deploy your reports to JReport Enterprise Server. When you schedule the report, please make sure you have the Enable Style Group option enabled and a style group selected.

Using Styles in Your Report

JReport styles can simplify your work in a lot of aspects, now let's use a simple example to illustrate how to use JReport styles and style groups in your report. 

  1. Start JReport Designer, open the existing report invoice.cls in Tutorial.cat. We are going to apply styles to this report.

  2. Open Catalog Browser, switch to the Styles tab. Create a new style group called Red under style tree root.
  3. Create a new style with the following information:

    StyleName: Title
    StyleType:  Label
    Description:  [You may leave this field blank.]

    Add the property entries and fill their values as follow:

    Bold: true
    FontSize: 30
    FontFace: Times New Roman
    Alignment: LeftCenter
    Italic: true
    Background: Transparent
    Foreground: 0xfc5e03

    Create other new styles using the information below:

    StyleName: Line
    StyleType: Line
    LineColor: 0xfc5e03


    StyleName: lblFieldName
    StyleType: Label 
    FontFace: Verdana
    FontSize: 7
    Bold: true
    Background: 0xfc5e03
    Foreground: 0xffffff

     

    StyleName: Box
    StyleType: Box
    BorderColor: 0xfc5e03

  4. Open Report Inspector, select invoice report root, scroll to find StyleGroup property entry in the Property Sheet, type Red in the value field.
  5. Select the report title label Invoice on the report design canvas, and then type Title for the Style property field in Report Inspector.
  6. Select field name labels Bill to, Ship to, Cust ID, No., Order Date, Required Date, Ship Date, Ship Via, Terms, Item, Product No., Unit Price, Quantity, Amount, and Total, and then type lblFieldName for the Style property field in Report Inspector. Use the CTRL key to select multiple objects. If you have difficulties in selecting the objects, please use Report Inspector as an aid.
  7. In Report Inspector tree view, select all the line objects, and then type Line for the Style property field in the Property Sheet of Report Inspector. To select consecutive objects in Report Inspector, select the first object, hold down the SHIFT key, and then click the last item.
  8. In Report Inspector tree view, select the box object, and then type Box for the Style property field in the Property Sheet of Report Inspector.
  9. Now we are going to create a new style group called Green. In Catalog Browser, right-click Red style group, and then click Copy Style Group. Rename the new group to Green, double-click group Green to show the Property Sheet, and then modify the information of each style in using the below information:

    Title
    FontFace: Arial Black
    FontSize: 25
    Foreground: 0xfc5e03
     
    Line
    LineColor: 0xfc5e03
     
    Box
    BorderColor: 0xfc5e03
     
    lblFieldName
    Bold: true
    FontSize: 7
    FontFace: Arial
    Background: 0xfc5e03
    Foreground: 0x000000
  10. Save the report and catalog.
  11. Start JReport Enterprise Server, and then deploy the report Invoice.cls together with the catalog Tutorial to the server.
  12. Visit the URL http://localhost:8888 to access the AccessDemo catalog, point to the report Invoice and then pick Advanced Run.
  13. On the View Format tab, check Enable Style Group, select Red from the drop down list to the right, and then click Submit. The report may somewhat look like the below picture.


  14. Run the report again, this time choose the Green group as the style group.

Limitations

The subreport in the main report works as a report object and can be applied with a Subreport type style. That is to say, the original styles (if any) applied to an individual report works no longer after this report is inserted into another report as a subreport, instead, you can apply a Subreport type style to the inserted subreport to control its appearance and presentation, just like what you do with a normal dbfield.

Index