Blank lines in title causes StringIndexOutOfBoundsException

A free public discussion forum for the JFreeChart class library.

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(String.java:1938)
at org.jfree.text.G2TextMeasurer.getStringWidth(G2TextMeasurer.java:80)
at org.jfree.text.TextUtilities.nextLineBreak(TextUtilities.java:294)
at org.jfree.text.TextUtilities.createTextBlock(TextUtilities.java:235)
at org.jfree.chart.title.TextTitle.arrangeRR(TextTitle.java:632)
at org.jfree.chart.title.TextTitle.arrange(TextTitle.java:501)
at org.jfree.chart.JFreeChart.drawTitle(JFreeChart.java:1321)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1209)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1668)
at javax.swing.JComponent.paint(JComponent.java:1027)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:277)
at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
at javax.swing.JComponent.paint(JComponent.java:1013)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


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.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class PieChartDemo1 extends ApplicationFrame
{

    public PieChartDemo1(String s)
    {
        super(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");
        pieplot.setCircular(false);
        pieplot.setLabelGap(0.02D);
        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") {
            @Override
            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");
                pieplot.setCircular(false);
                pieplot.setLabelGap(0.02D);
                i++;
                i = i % 3;
               
            }
        });


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

    public static void main(String args[])
    {
        PieChartDemo1 piechartdemo1 = new PieChartDemo1("JFreeChart: PieChartDemo1.java");
        piechartdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(((java.awt.Window) (piechartdemo1)));
        piechartdemo1.setVisible(true);
    }
}
grimlock
 
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:

https://sourceforge.net/tracker/?func=d ... 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
david.gilbert
JFreeChart Project Leader
 
Posts: 11352
Joined: Fri Mar 14, 2003 10:29 am


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 10 guests