The error message (NotSerializableException) and corresponding stack trace offered little in the way of clues. I finally figured out that the problem was with an interface implementation I had set for generating custom legend item labels. I used a lambda expression to supply the interface (XYSeriesLabelGenerator) implementation. Alternatively, one might use an anonymous inner class to provide the implementation. Neither of these approaches will work, as it is not possible (as far as I know) to implement the Serializable interface for the implementation. So, I used the good, old, named top-level class:
Code: Select all
public class CustomSeriesLabelGenerator implements XYSeriesLabelGenerator,
Serializable {
@Override
public String generateLabel(XYDataset dataset, int seriesIndex) {
String label;
try {
XYSeriesCollection collection = (XYSeriesCollection) dataset;
label = collection.getSeries(seriesIndex).getDescription();
} catch (ClassCastException cce) {
label = "Series" + seriesIndex;
}
return label;
}
}
Code: Select all
XYItemRenderer renderer = ...;
renderer.setLegendItemLabelGenerator(new CustomSeriesLabelGenerator());