Search code examples
javanetbeansjfreechart

How To Add a jFreeChart to a jPanel in a jFrame?


I've looked at lots of questions/answers that are similar and found one which is exactly the same, 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.

Here is the contained 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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    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();
    }// </editor-fold>                        

    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                   
}

Solution

  • You never add your ChartPanel to your JFrame.

    image

    As tested:

    import java.awt.Dimension;
    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() {
            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);
            ChartPanel cp = new ChartPanel(chart) {
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(320, 240);
                }
            };
            cp.setMouseWheelEnabled(true);
            add(cp);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
        }
    
        public static void main(String args[]) {
    
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new ChartTest().setVisible(true);
                }
            });
        }
    }