1

I am trying to make a chart that is embedded specifically in a panel on a JInternalFrame; it is a pie chart from the library JFreeChart. I want to embed the chart on a panel which goes by the variable name jpPaneles, but it has proven to be impossible. This is really crucial for my project, so if anyone has the time to help me out, I would greatly appreciate it. I am working in NetBeans GUI editor. Here is the code and you can see I try to add frame1 to a panel.

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;

public class Eventad extends javax.swing.JInternalFrame {

    public Eventad() {
        initComponents();
    }

    public void updateChart() {
    }

    public static void main(String arg[]) {
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        pieDataset.setValue("One", new Integer(10));
        pieDataset.setValue("Two", new Integer(20));
        pieDataset.setValue("Three", new Integer(30));
        pieDataset.setValue("Four", new Integer(10));
        pieDataset.setValue("Five", new Integer(20));
        pieDataset.setValue("Six", new Integer(10));
        JFreeChart chart = ChartFactory.createPieChart3D(
            "3D Pie Chart", pieDataset, true, true, true);
        PiePlot3D p = (PiePlot3D) chart.getPlot();
        p.setForegroundAlpha(0.5f);

        ChartFrame frame1 = new ChartFrame("3D Pie Chart", chart);
        frame1.setVisible(true);
        frame1.setSize(200, 200);
        //Here im trying to add the frame1 to the Jpanel
        this.jpPaneles.add(frame1);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cesar Downs
  • 57
  • 1
  • 3
  • 10

1 Answers1

9

Use your chart to create a ChartPanel and add the ChartPanel to the JInternalFrame. Add the JInternalFrame to a JDesktopPane. See How to Use Internal Frames for more.

Addendum: For example,

InternalPie

import java.awt.EventQueue;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

/** @see http://stackoverflow.com/questions/8199766 */
public class InternalPie {

    private void display() {
        JFrame f = new JFrame("InternalPie");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DefaultPieDataset pieDataset = new DefaultPieDataset();
        pieDataset.setValue("One", new Integer(10));
        pieDataset.setValue("Two", new Integer(20));
        pieDataset.setValue("Three", new Integer(30));
        pieDataset.setValue("Four", new Integer(10));
        pieDataset.setValue("Five", new Integer(20));
        pieDataset.setValue("Six", new Integer(10));
        JFreeChart chart = ChartFactory.createPieChart3D(
            "3D Pie Chart", pieDataset, true, true, true);
        ChartPanel cp = new ChartPanel(chart);

        JInternalFrame jif = new JInternalFrame(
            "Chart", true, true, true, true);
        jif.add(cp);
        jif.pack();
        jif.setVisible(true);

        JDesktopPane dtp = new JDesktopPane();
        dtp.add(jif);
        f.add(dtp);
        f.pack();
        f.setSize(700, 500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new InternalPie().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045