Netbeans: adding to jPanel via code after init (jFreeChart)

This will be my second post. Many thanks to those who replied to my first questions.

I am using netbeans 5.5

I am attempting to run the following jFreeChart demo but in netbeans.

Original demo located here: http://www.java2s.com/Code/Java/Chart/JFreeChartPieChartDemo7.htm

Instead of programically creating the jPanel like the demo does, i would like to do it using the netbeans GUI and then add the code to create the jFreeChart.

In netbeans i create a new project, create a new jFrame and place a new jPanel on the form (keeping the variable name jPanel1) as illustrated below:

/*

* MainForm.java

*

* Created on June 28, 2007, 1:48 PM

*/

package mypkg;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.PiePlot;

import org.jfree.chart.plot.PiePlot3D;

import org.jfree.data.general.DefaultPieDataset;

import org.jfree.ui.ApplicationFrame;

import org.jfree.ui.RefineryUtilities;

/**

*

* @author me

*/

publicclass MainFormextends javax.swing.JFrame{

/** Creates new form MainForm */

public MainForm(){

initComponents();

}

/** Netbeans Auto-Generated Code goes here which I have omitted */

/**

* @param args the command line arguments

*/

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new MainForm().setVisible(true);

}

});

}

privatevoid CreateChart(){

DefaultPieDataset dataset =new DefaultPieDataset();

dataset.setValue("Section 1", 23.3);

dataset.setValue("Section 2", 56.5);

dataset.setValue("Section 3", 43.3);

dataset.setValue("Section 4", 11.1);

JFreeChart chart3 = ChartFactory.createPieChart3D("Chart 3", dataset, false, false,false);

PiePlot3D plot3 = (PiePlot3D) chart3.getPlot();

plot3.setForegroundAlpha(0.6f);

plot3.setCircular(true);

jPanel1.add(new ChartPanel(chart3));

jPanel1.validate();

}

// Variables declaration - do not modify

private javax.swing.JPanel jPanel1;

// End of variables declaration

}

I then attempted to call the CreateChart() function in several places (in the MainForm() function, in the pre and post creation code properties of the jPanel's properties dialog in netbeans (as well as pre and post init in the same screen)) and nothing seems to work. the program compiles and the jFrame comes up but the chart does not show.

If i run the example at the above noted link it runs fine in netbeans if i copy and paste the code directly, it just doesnt work when i try to get it to work through the netbeans GUI builder and the jPanel i create through it.

Can any more advanced netbeans/java users lend me a hand or point me in the right direction?

NOTE: in the code pasted above, i am only trying to run one of the four chart demo's listed in the original source in the link.

NOTE2: in the code pasted above it doesnt show me calling CreateChart() from anywhere, im hoping to get some advice on how to proceed from here.

Message was edited by:

amadman

[4912 byte] By [amadmana] at [2007-11-27 9:11:00]
# 1
can you see if the jPanel1 is ever added to the contentPane of the MainForm? You should see something like:getContentPane().add(jPanel1); or something similar. Is it there?
petes1234a at 2007-7-12 21:56:10 > top of Java-index,Java Essentials,New To Java...
# 2

Here is the netbeans generated code that i had omitted in the post above:

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

CreateChart(); //this is one of the many places i tried sticking the function

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 546, Short.MAX_VALUE)

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 398, Short.MAX_VALUE)

);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

pack();

}// </editor-fold>

I believe this is what you mentioned looking for, or close to it:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

any thoughts?

amadmana at 2007-7-12 21:56:10 > top of Java-index,Java Essentials,New To Java...
# 3

I tried adding a jButton in netbeans just to see what changes in the netbeans generated code and starting to think that maybee, in order to do this through netbeans i have to use the "Custom Creation Code" field in properties to generate the code to build the jPanel myself.

*shrug*

i'm trying, lol, i dont give up easily.

amadmana at 2007-7-12 21:56:10 > top of Java-index,Java Essentials,New To Java...
# 4
my recommendation: scrap the netbean generated swing code and hand code it yourself. :)
petes1234a at 2007-7-12 21:56:10 > top of Java-index,Java Essentials,New To Java...
# 5

yeah, i guess thats what im gonna have to do. im just dissapointed because im trying to grasp a good fundamental understanding of the netbeans IDE and if stuff like this restricts it what is the poing of it anyways? i know there is a way to handle this through the netbeans IDE i just dont know it, and search upon search upon search has yielded little result.

oh well, life goes on. eventually i'll play with eclipse, and further down the road maybee i'll just hand code the GUI's as a whole.

im taking the "walk, step, run" approach. heh.

thanks for the input tho.

amadmana at 2007-7-12 21:56:10 > top of Java-index,Java Essentials,New To Java...
# 6
My previous recommendation is a personal preference you understand. You may do well to try to explore this issue on a Netbeans forum, if you can find one. I did a google search and did find this one here: http://www.nabble.com/Netbeans.org-f2602.htmlAgain, good luck.
petes1234a at 2007-7-12 21:56:10 > top of Java-index,Java Essentials,New To Java...
# 7

Though I guess I am already a bit late:

You could have gone along and did it the following way. Let JFreeChart create an image, and add that image to a JLabel.

BufferedImage image = chart.createBufferedImage(500,300);

JLabel lblChart = new JLabel();

lblChart.setIcon(new ImageIcon(image));

bhentgesa at 2007-7-12 21:56:10 > top of Java-index,Java Essentials,New To Java...