I’m fairly new to Java and I’ve been trying to put a JFreeChart in a JPanel. I’ve finished the tutorial exercises where you generate a simple chart (with its own application window):
TimeSeriesChartDemo1
However I’m working on a larger group project that requires me to put the chart to be in a JPanel rather than its own window. The problem is I have no idea how to move from here, I also do not have the NetBeans IDE.
- What do I have to put in
initComponents()to initialize the JPanel? - Do I still have two files
JFreeChartDemo.javaandNetBeansGUI.javaor is it all in one file?
If more information is needed please comment below.
For reference, I’ve cited a generic XYPlot example:
- API:
TimeSeriesChartDemo1 - Source:
TimeSeriesChartDemo1

In your
initComponentsjust initialize the JPanel (you can find plenty of information here). Once you have done that, all that you should do would be to add your chart to theJPanel. Once you have done that, just add theJPanelto theJFrameyou are working with.So I am assuming you have something like so:
public class JFreeChartDemo extends JFrameYou should be able to get most of the work done if you do this:
public class JFreeChartDemo extends JPanelThis should allow you to re-use most of the initialization code, such as setting the size and location of the panel. You will however have to remove any JFrame related code from there.
Finally, you should keep two seperate source files, one for the chart and one for the rest. I am assuming that in the
NetBeansGUIclass you are doing the rest of the GUI stuff, so you in yourinitComponent(inNetBeansGUI) you could also have:In this case I am also assuming that
NetBeansGUIextendsJFrame.