Search code examples
javaswingcomponentsjfreechart

ChartPanel not registering as a component in a GUI


I have the following output.

Recursive call for org.jfree.chart.ChartPanel[chartpanelBoa
Component count 0 for org.jfree.chart.ChartPanel[chartpanelBoa

The code i run to get this output is

public void enableEverything(Container c){
Component [] p = c.getComponents();
System.out.println("Component count " + c.getComponentCount() + " for " +   
                                c.toString().substring(0,40)  );
for(Component pp : p){
pp.setEnabled(true);
if(pp instanceof Container){
System.out.println("Recursive call for " + pp.toString().substring(0,40));
enableEverything((Container) pp);
}
else System.out.println("No recursive call");
}



}

The ChartPanel can be be seen in my JPanel, however it wont count the chartpanel. I am expecting an output of

Component count 1 for org.jfree.chart.ChartPanel[chartpanelBoa

What sits behind the scenes that counts the component?


Solution

  • As shown in the source for org.jfree.chart.ChartPanel, a ChartPanel is a JPanel, but it contains no nested instances of Component. A handful of Swing components are imported to handle a context menu, tooltips, etc., but the enclosed JFreeChart itself is pure Java2D.

    It looks like you want to alter the chart's visual representation when it is disabled. You can still invoke setEnabled() on the ChartPanel itself to leverage the JPanel UI delegate. You can also modify the interior of the chart as desired; this example dims the background using setBackgroundPaint() or setBackgroundImageAlpha().