0

Im using netbeans gui editor and im trying to add a Jfreechart that is itself in a internal frame, and this internal frame I am wanting to add it to a panel, as you can see in this image (sorry I cant post image directly because im a newbie):

http://www.flickr.com/photos/63259070@N06/6370734167/

The internal frame doesn't even show up on the panel "Estadisticas" when I run it, I think its harder because im not doing the gui by code but it shouldn't be that hard, If anyone could help me add this properly I would greatly appreciate it, here is the code that I have been trying out:

 private void display() {
       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);
    this.ji.add(cp); //ji is the name of the internal frame
    this.ji.pack();
    this.ji.setVisible(true);
    this.ji.setSize(100, 100);

    JDesktopPane dtp = new JDesktopPane();
    dtp.add(ji);
    this.jpEstadisticas.add(dtp);   //jpEstadisticas the name of the main "Estadisticas"panel

}
Cesar Downs
  • 57
  • 1
  • 3
  • 10

4 Answers4

1

You did not add dtp to the JFrame's content pane. You can use the UI editor of NetBeans.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • You must have something wrong with the usage of JDesktopPane/JInternalFrame. See http: //www.roseindia.net/java/example/java/swing/AllFrameDesktopContainer.shtml (added a space after http: ) Otherwise try to show a simple JLabel. – Joop Eggen Nov 22 '11 at 12:14
0

I think following code will work for you :

import org.jfree.chart.ChartPanel;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;

public class PieChartJFrame extends javax.swing.JFrame {

/** Creates new form PieChartJFrame */
ChartPanel chartPanel;
public PieChartJFrame() {
    initComponents();
}

private PieDataset createPieDataSet() {

    DefaultPieDataset pieDataset = new DefaultPieDataset();
    pieDataset.setValue("Othes", new Integer(15));
    pieDataset.setValue("PHP", new Integer(15));
    pieDataset.setValue("Java", new Integer(30));
    pieDataset.setValue("Perl", new Integer(10));
    pieDataset.setValue("C,C++,C#", new Integer(30));

    return pieDataset;

}

private JFreeChart create3DPieChart(PieDataset dataset){

    /** Create a PieDataSet* */


    /** Create 3D Pie Chart based on this dataset* */
    JFreeChart chart = ChartFactory.createPieChart3D(
            "Popularity of Languages", dataset, true, true, true);

    return chart;


}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jInternalChartFrame = new javax.swing.JInternalFrame();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setName("Form"); // NOI18N

    jInternalChartFrame.setName("jInternalChartFrame"); // NOI18N
    jInternalChartFrame.setVisible(true);

    javax.swing.GroupLayout jInternalChartFrameLayout = new javax.swing.GroupLayout(jInternalChartFrame.getContentPane());
    jInternalChartFrame.getContentPane().setLayout(jInternalChartFrameLayout);
    jInternalChartFrameLayout.setHorizontalGroup(
        jInternalChartFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 301, Short.MAX_VALUE)
    );
    jInternalChartFrameLayout.setVerticalGroup(
        jInternalChartFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jInternalChartFrame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(634, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jInternalChartFrame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(300, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */

// Variables declaration - do not modify                     
private javax.swing.JInternalFrame jInternalChartFrame;
// End of variables declaration                   

private void display(){

    final PieDataset dataset = this.createPieDataSet();
    final JFreeChart chart   = this.create3DPieChart(dataset);

    ChartPanel chartPanel = new ChartPanel(chart, false);
    this.jInternalChartFrame.setContentPane(chartPanel);
    this.jInternalChartFrame.pack();
    this.jInternalChartFrame.setVisible(true);
    this.jInternalChartFrame.setSize(100, 100);

    this.pack();
    this.setVisible(true);

}

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {

            PieChartJFrame pieChart = new PieChartJFrame();

            pieChart.display();

        }
    });
}
}
shivshankar
  • 691
  • 2
  • 14
  • 31
0

Well the problem is that the NetBeans GUI editor will generate the initComponents() method for you where it does all the configuration of layouts etc. Now you cannot modify this method yourself because NetBeans won't let you and even if you modify it outside the IDE it will change it back to the original form. But there is a way to modify parts of the code, which should work for this case. GETah was right. Add a panel to your frame just like you would add any other component in the editor. Right click on it and select Customize Code. Now there should be a line looking something like this:

jPanel1 = new javax.swing.jPanel();

Next to that line should be a dropdown menu where you can choose between default code and custom creation. You want to select custom creation and change the line to this:

jPanel1 = cp;

Should work now, does for me.

P.M.
  • 98
  • 5
-1

Don't add the chart panel into the main frame, add it to its content pane instead. replace this.ji.add(cp); by this.ji.getContentPane().add(cp)

Or better: In NetBeans, under the GUI editor (not code editor) add a panel into your main frame and call it something like chartPanel. Add all other controls you want to display and position them as you like. Once done, switch back to code editor. On the main frame's constructor, do the following:

// Inside the initializeComponents() method 
// Find and replace 
chartPanel = new JPanel(); 
// By 
chartPanel = createChartPanel();

// Create chart panel method
public JPanel createChartPanel(){
    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);
    return new ChartPanel(chart);
}
GETah
  • 20,922
  • 7
  • 61
  • 103
  • Ah yes, I forgot, open the file in notepad, edit and save it :) – GETah Nov 20 '11 at 19:22
  • Nope nothing... is there anyway I could send my project, and help me out please!, that is the only thing that is bugging me – Cesar Downs Nov 20 '11 at 19:41
  • I am more than happy to have a look a deep look at your code, please edit your question and put all your JFrame code there. – GETah Nov 20 '11 at 19:48
  • Never mind I finally got it thanks mate!.. ive giving you more reputation :) – Cesar Downs Nov 20 '11 at 20:00
  • Nothing, it was dumb me I didnt implement the method createChartPanel() although now im having issues with re sizing the chart.. :( – Cesar Downs Nov 20 '11 at 20:23
  • Good that you fixed it. You can always post a separate question on the sizing issue and post the link here so that I can have a look at it:) – GETah Nov 20 '11 at 20:25
  • Here ya go mate: http://stackoverflow.com/questions/8204544/how-to-re-size-this-chart-in-jfreechart-netbeans – Cesar Downs Nov 20 '11 at 20:40