Jump to content

Resizing text elements dynamically


Recommended Posts



I have implemented a dynamic column table report using the JRDesign API, and have found that the amount of text of some of the column headers is too big to fit in the static text elements I have constructed.


I was wondering if there is anyway to find the length of the string in the text elements, or if anyone knows how to resize a text element so that it will allow the whole string to display?




Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Yes that would fix a problem in the short term but what happens if the textbox overflows onto another column header textbox? Will that happen if a long string?


What I would like to do is to build the textboxes depending on the length of the strings, or on where the last text box ended.

Link to comment
Share on other sites

jimiohara wrote:

I was wondering if there is anyway to find the length of the string in the text elements


You can use the internal JasperReports text measurer to determine the required height for a static text. See the following sample code:


JasperDesign jasperDesign = new JasperDesign();

JRDesignStyle style = new JRDesignStyle();

JRDesignStaticText text = new JRDesignStaticText(jasperDesign);
text.setText("I was wondering if there is anyway to find the length of the string in the text elements"«»);

Map attributes = new HashMap();
attributes.putAll(JRFontUtil.setAttributes(attributes, text));
JRStyledText styledText =
JRStyledTextParser.getInstance().getStyledText(attributes, text.getText(), text.isStyledText());

JRMeasuredText measuredText =
JRTextMeasurerUtil.createTextMeasurer(text).measure(styledText, 0, jasperDesign.getPageHeight(), false);
text.setHeight((int) Math.ceil(measuredText.getTextHeight()));

JRDesignBand title = new JRDesignBand();





Link to comment
Share on other sites

  • 2 months later...



Is this API accessible from within an already compiled report? Otherwise, how can horizontal dynamic text width resize be accomplished?


What I'm trying to do is to have a non-transparent text on top of the top line of a rounded rectangle, creating a great grouping effect (as a generic subreport). Because the text is non transparent, the width of the element must be a function of the width of the actual text used.


Does this make sense? Is it possible at all? size=397]http://www.jasperforge.org/components/com_joomlaboard/uploaded/images/border.jpg

Post edited by: edensh, at: 2008/05/23 21:31

Link to comment
Share on other sites

JasperReports text fields only stretch vertically, the horizontal axis layout is pretty much fixed (with the exception of crosstabs) when running reports.


You can change element widths in compiled reports (via JRElement.setWidth), but you won't be able to use the JR text measurer (without modifying/extending it) to determine how wide a specific text is.


A solution might be to create a wide transparent text field, and use styled text to set a text background:


<textElement textAlignment="Center" isStyledText="true"/>
"<style backcolor="white">" + $P{TITLE} + "</style>"




Post edited by: lucianc, at: 2008/05/27 07:56

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...