Mickey Mickey - 5 months ago 24
Java Question

JasperReports 6.0.3: JRDesignTextField.setFont and JasperDesign.getFontsMap is deprecated and removed

I'm trying to set font on JRDesignTextField object of a JasperReport from the one I have in JasperDesign's getFontMap() like this:

JRDesignTextField text; // I have this object
JasperDesign jasperDesign; //I have this object from a master jrxml template
text.setFont((JRFont)jasperDesign.getFontsMap().get("ColumnHeadingFont"));


Upgrading to JasperReports 6.0.3, setting font on JRDesignTextField and getting FontMap from JasperDesign are flagged as "deprecated".
After digging in little bit, I did some workaround to adapt this code which I'm not sure is correct:

JRDesignTextField text; // I have this object
JasperDesign jasperDesign; //I have this object
text.setFontName("ColumnHeadingFont");
List<JRStyle> stylesList = jasperDesign.getStylesList();
for(JRStyle st : stylesList){
if("ColumnHeadingFont".equals(st.getFontName()))
{
text.setFontSize(st.getFontsize());
break;
}
}


So the problem is divided into two:


  • Get FontMap from JasperDesign object

  • Setting Font on JRDesignTextField (which I'll get from FontMap)



Is the way I proposed for this problem is correct and if there is any better way to do that?

Answer

So the solution I proposed is the feasible one... To get the style details(font size) from the master template and use that in designing new template's textField. Code is as below:

JRDesignTextField text; // This text field is from the new jasper report file
JasperDesign jasperDesign; //This is from master template that has all info

text.setFontName("ColumnHeadingFont");
List<JRStyle> stylesList = jasperDesign.getStylesList();
for(JRStyle st : stylesList){
if("ColumnHeadingFont".equals(st.getFontName()))
{
    text.setFontSize(st.getFontsize());
    break;
}
}
Comments