Nelda.techspiress
Nelda.techspiress

Reputation: 643

Exception when using POI FontFormatting setFontColor

Trying to use POI conditional formatting. I have the following method:

private final static Color PEAK_ORANGE = new Color(255, 239, 221);

public ConditionalFormattingRule getConditionalFormatting(ConditionalFormattingRule formattingContainer, FormatSpecs format){

    FontFormatting fontFmt = formattingContainer.createFontFormatting();
    fontFmt.setFontStyle(true, false);
    // fontFmt.setFontColorIndex((short)11);
    fontFmt.setFontColor(new XSSFColor(PEAK_ORANGE));

    PatternFormatting patternFmt = formattingContainer.createPatternFormatting();
    patternFmt.setFillBackgroundColor(new XSSFColor(PEAK_ORANGE));
    return formattingContainer;
}

When I use the setFontColor() method I get index out of bounds exception.

enter image description here

When I use the setFontColorIndex() method using some arbitrary index value, I do not get the exception. Notice however that I use the exact same color reference in the call to set the background color

patternFmt.setFillBackgroundColor(new XSSFColor(PEAK_ORANGE));

and this works fine, no exceptions.

Has anyone else run into this? Am I missing something in the call to set the font color? I prefer to use my colors rather than those from the IndexedColors class.

Upvotes: 1

Views: 304

Answers (1)

centic
centic

Reputation: 15872

This looks like a small bug in Apache POI, a simple workaround is to first set an index-color and then set the actual intended full color, i.e.

    fontFmt.setFontColorIndex((short)1);
    fontFmt.setFontColor(new XSSFColor(PEAK_ORANGE));

The setFontColorIndex() will initialize the internal structures so that setFontColor() will work.

FYI, the bug should be fixed in time for release 4.0 of Apache POI.

Upvotes: 3

Related Questions