title: How To Add a jFreeChart to a jPanel in a jFrame?
body: <p>I've looked at lots of questions/answers that are similar and found one which is <a href="https://stackoverflow.com/questions/20297961/how-to-add-chartpanel-in-jpanel-inside-a-jframe">exactly the same</a>, but I have not managed to get my program to work. The output is a blank JFrame, it should output a JFrame with a graph(100x200) inside it.</p>
  
  <p>Here is the contained code:</p>
  
  <pre><code>package my.Project;
  
  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import javax.swing.JPanel;
  import org.jfree.chart.ChartFactory;
  import org.jfree.chart.ChartPanel;
  import org.jfree.chart.JFreeChart;
  import org.jfree.chart.plot.PlotOrientation;
  import org.jfree.data.xy.XYDataset;
  import org.jfree.data.xy.XYSeries;
  import org.jfree.data.xy.XYSeriesCollection;
  
  public class ChartTest extends javax.swing.JFrame {
  
      public ChartTest() {
          try {
  
              XYSeries Goals = new XYSeries("Goals Scored");
              Goals.add(1, 1.0);
              Goals.add(2, 3.0);
              Goals.add(3, 2.0);
              Goals.add(4, 0.0);
              Goals.add(5, 3.0);
  
              XYDataset xyDataset = new XYSeriesCollection(Goals);
  
              JFreeChart chart = ChartFactory.createXYLineChart("Goals Scored Over Time", "Fixture Number", "Goals", xyDataset, PlotOrientation.VERTICAL, true, true, false);
  
              JPanel jPanel1 = new JPanel();
              jPanel1.setLayout(new java.awt.BorderLayout());
              jPanel1.setVisible(true);
              jPanel1.setSize(300, 300);
  
              ChartPanel CP = new ChartPanel(chart);
              CP.setPreferredSize(new Dimension(100, 200));
              CP.setMouseWheelEnabled(true);
  
              jPanel1.add(CP, BorderLayout.CENTER);
              jPanel1.validate();
  
          } catch (Exception e) {
              System.out.print("Chart exception:" + e);
          }
          initComponents();
      }
  
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 300, Short.MAX_VALUE)
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 400, Short.MAX_VALUE)
          );
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
      public static void main(String args[]) {
  
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new ChartTest().setVisible(true);
              }
          });
      }
      // Variables declaration - do not modify                     
      // End of variables declaration                   
  }
  </code></pre>