HOW-TO Use inherited styles

A great feature of JasperReport is styles inheritance, such feature is supported by DynamicJasper also. A style can have a parent style, for all non defined properties in a style, parents one will be used.
In the following example we create a style “subtitleParent” to be used as parent and a “subtitleStyle” to be used as a child.

Style subtitleStyleParent = new Style("subtitleParent");
subtitleStyleParent.setBackgroundColor(Color.CYAN);
subtitleStyleParent.setTransparency(Transparency.OPAQUE);

Style subtitleStyle = Style.createBlankStyle("subtitleStyle","subtitleParent");
subtitleStyle.setFont(Font.GEORGIA_SMALL_BOLD);

...

DynamicReportBuilder drb = new DynamicReportBuilder();
drb.setTitle("November 2006 sales report")     //defines the title of the report
        .setSubtitle("The items in this report correspond "
                        +"to the main products: DVDs, Books, Foods and Magazines")
        .setDefaultStyles(titleStyle, subtitleStyle, headerStyle, null)
        .addStyle(subtitleStyleParent) //registers parent style

...

Note that in this example we are using a factory method of the Style class: Style.createBlankStyle(…), this is useful for creating styles that are going to have parent because most of its properties are set to null.

The styles created with the operator new (new Style()) have default values for almost everything and this makes useless for the style inheritance mechanism, since a style uses its parent values for all non defined values

Refer to TemplateStyleReportTest for a working example.

Fork me on GitHub