Blank lines in title causes StringIndexOutOfBoundsException

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

Blank lines in title causes StringIndexOutOfBoundsException

Postby grimlock » Tue Jul 07, 2009 9:01 am

This modified version of the Pie Chart 3D Demo 1 causes the following exception. I added three consecutive \n to the title of the chart. Two consecutive \n are okay, and if there are characters in between the \n it works as well. Since this is the code for TextTitle, it affects any chart that uses TextTitle as the title.

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(
at org.jfree.text.G2TextMeasurer.getStringWidth(
at org.jfree.text.TextUtilities.nextLineBreak(
at org.jfree.text.TextUtilities.createTextBlock(
at org.jfree.chart.title.TextTitle.arrangeRR(
at org.jfree.chart.title.TextTitle.arrange(
at org.jfree.chart.JFreeChart.drawTitle(
at org.jfree.chart.JFreeChart.draw(
at org.jfree.chart.ChartPanel.paintComponent(
at javax.swing.JComponent.paint(
at javax.swing.JComponent.paintChildren(
at javax.swing.JComponent.paint(
at javax.swing.JComponent.paintChildren(
at javax.swing.JComponent.paint(
at javax.swing.JLayeredPane.paint(
at javax.swing.JComponent.paintChildren(
at javax.swing.JComponent.paintToOffscreen(
at javax.swing.BufferStrategyPaintManager.paint(
at javax.swing.RepaintManager.paint(
at javax.swing.JComponent.paint(
at java.awt.GraphicsCallback$
at sun.awt.SunGraphicsCallback.runOneComponent(
at sun.awt.SunGraphicsCallback.runComponents(
at java.awt.Container.paint(
at javax.swing.RepaintManager.paintDirtyRegions(
at javax.swing.RepaintManager.paintDirtyRegions(
at javax.swing.RepaintManager.seqPaintDirtyRegions(
at javax.swing.SystemEventQueueUtilities$
at java.awt.event.InvocationEvent.dispatch(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(

Code: Select all
package demo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class PieChartDemo1 extends ApplicationFrame

    public PieChartDemo1(String s)
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(((java.awt.Container) (jpanel)));

    private static PieDataset createDataset()
        DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
        defaultpiedataset.setValue("One", ((Number) (new Double(43.200000000000003D))));
        defaultpiedataset.setValue("Two", ((Number) (new Double(10D))));
        defaultpiedataset.setValue("Three", ((Number) (new Double(27.5D))));
        defaultpiedataset.setValue("Four", ((Number) (new Double(17.5D))));
        defaultpiedataset.setValue("Five", ((Number) (new Double(11D))));
        defaultpiedataset.setValue("Six", ((Number) (new Double(19.399999999999999D))));
        return ((PieDataset) (defaultpiedataset));

    private static JFreeChart createChart(PieDataset piedataset)
        JFreeChart jfreechart = ChartFactory.createPieChart("Pie Chart Demo 1\n\n\ntestnew line", piedataset, true, true, false);
        TextTitle texttitle = jfreechart.getTitle();
        texttitle.setToolTipText("A title tooltip!");
        PiePlot pieplot = (PiePlot)jfreechart.getPlot();
        pieplot.setLabelFont(new Font("SansSerif", 0, 12));
        pieplot.setNoDataMessage("No data available");
        return jfreechart;

    static int i = 0;

    public static JPanel createDemoPanel()
        JPanel panel = new JPanel(new BorderLayout());
        final JFreeChart jfreechart = createChart(createDataset());
        final ChartPanel cp = new ChartPanel(jfreechart);
        panel.add(cp, BorderLayout.CENTER);

        JButton button = new JButton(new AbstractAction("Switch") {
            public void actionPerformed(ActionEvent evt) {
                PiePlot pp = (PiePlot) jfreechart.getPlot();
                if (i == 0) {
                    cp.setChart(new JFreeChart(new PiePlot3D(pp.getDataset())));
                } else if (i == 1) {
                    cp.setChart(new JFreeChart(new RingPlot(pp.getDataset())));
                } else if (i == 2) {
                    cp.setChart(new JFreeChart(new PiePlot(pp.getDataset())));
                PiePlot pieplot = (PiePlot)cp.getChart().getPlot();
                pieplot.setLabelFont(new Font("SansSerif", 0, 12));
                pieplot.setNoDataMessage("No data available");
                i = i % 3;

        panel.add(button, BorderLayout.SOUTH);
        return panel;

    public static void main(String args[])
        PieChartDemo1 piechartdemo1 = new PieChartDemo1("JFreeChart:");
        RefineryUtilities.centerFrameOnScreen(((java.awt.Window) (piechartdemo1)));
Posts: 22
Joined: Wed Jul 14, 2004 7:50 am

Re: Blank lines in title causes StringIndexOutOfBoundsException

Postby david.gilbert » Tue Jul 07, 2009 9:34 pm

Thanks for the report. Definitely a bug...but the code is making my head hurt. I've opened a bug report to track it: ... tid=115494
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
JFreeChart Project Leader
Posts: 11664
Joined: Fri Mar 14, 2003 10:29 am

Return to JFreeChart

Who is online

Users browsing this forum: No registered users and 9 guests