How to connect a JPanel created by JFreeChart to JFrame

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lucky7456969
Posts: 15
Joined: Thu Nov 01, 2012 6:41 am
antibot: No, of course not.

How to connect a JPanel created by JFreeChart to JFrame

Post by lucky7456969 » Thu Nov 01, 2012 6:43 am

Dear boss,:d:
http://www.java2s.com/Code/Java/Chart/J ... tDemo1.htm

Code: Select all

 public bokss_patient_frame() {
        initComponents();
          this.setExtendedState(getExtendedState()|this.MAXIMIZED_BOTH);

         
// do whatever else you want to do ...
        this.setVisible(true);
        
        chartCanvas = new ChartCanvas();
        JPanel pan = chartCanvas.createDemoPanel();
        jPanel1 = pan;
        
        jPanel1.setVisible(true);
  
     
 }

Code: Select all

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boksspatientanalysis;

import java.awt.Font;

import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
/**
 *
 * @author Garfield
 */
public class ChartCanvas {
    
     private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));
        return dataset;        
    }
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return A chart.
     */
    private static JFreeChart createChart(PieDataset dataset) {
        
        JFreeChart chart = ChartFactory.createPieChart(
            "Pie Chart Demo 1",  // chart title
            dataset,             // data
            true,               // include legend
            true,
            false
        );

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;
        
    }
    
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }
    
    public ChartCanvas()
    {
         
    }
}
Sorry about the naming, Panel == Canvas here. No time to correct.
Thanks

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to connect a JPanel created by JFreeChart to JFrame

Post by david.gilbert » Thu Nov 01, 2012 10:32 am

This is really a question about Swing, since a ChartPanel is a JComponent, just as a JButton or JLabel is. So take a look through some Swing books or tutorials, and focus on layout managers to learn how you add components to a container, and have them laid out in the presentation that you need.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

lucky7456969
Posts: 15
Joined: Thu Nov 01, 2012 6:41 am
antibot: No, of course not.

Re: How to connect a JPanel created by JFreeChart to JFrame

Post by lucky7456969 » Thu Nov 01, 2012 11:27 am

Thanks David,
Rather than straight diving into books, I searched a few forums on this.
I should have done something like this
this.setContentPane(chartPanel);

And the "Canvas" for the chart should derive from ApplicationFrame.
I saw both of the example methods get called.
But there is still nothing coming on the screen.
Any advice would be greatly appreciated!
Thanks
Jack

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: How to connect a JPanel created by JFreeChart to JFrame

Post by John Matthews » Thu Nov 01, 2012 2:28 pm

Jack wrote

Code: Select all

this.setContentPane(chartPanel);
It's unlikely that you want to replace the content pane. Instead, note that add() has "been overridden to forward to the contentPane as necessary." See the API for details.

Also note that org.jfree.chart.demo.PieChartDemo1 is one of the demos, the current source for which is in the distribution.

Finally, this example adds a CharPanel to a JFrame.

Locked