Cannot show a chart on a Swing JPanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
vivian22
Posts: 13
Joined: Fri Nov 10, 2006 10:30 pm

Cannot show a chart on a Swing JPanel

Post by vivian22 » Tue Nov 14, 2006 9:27 pm

In the swing program, I wrote:

//create a dataset
CategoryDataset dataset = createDataset();

//create a chart
JFreeChart chart = createChart(dataset);

//create a chartPanel and set up the size
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(400, 270));

//add a chart panel to a JPanel1
jPanel1.add(chartPanel,CENTER_ALIGNMENT);

When I ran it, there is no chart shown on the swing JPanel. I don't know what the problem is. Is there anyone who can help me?
Thank you.

vivian22
Posts: 13
Joined: Fri Nov 10, 2006 10:30 pm

Post by vivian22 » Tue Nov 14, 2006 10:02 pm

Anybody experienced same situation?

forsey85
Posts: 17
Joined: Mon Jul 03, 2006 2:55 pm

Post by forsey85 » Wed Nov 15, 2006 12:49 pm

Where are you putting jPanel1? on a JFrame/JDialog?

vivian22
Posts: 13
Joined: Fri Nov 10, 2006 10:30 pm

Post by vivian22 » Wed Nov 15, 2006 2:24 pm

I put jpanel1 on a JFrame.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Wed Nov 15, 2006 4:39 pm

As a test, what happens if you replace this line

Code: Select all

 jPanel1.add(chartPanel,CENTER_ALIGNMENT);
with this

Code: Select all

 jPanel1.add(new JButton("foo"),CENTER_ALIGNMENT);
If the button does not appear where you expect, then the problem is not in the chartPanel but somewhere else.

vivian22
Posts: 13
Joined: Fri Nov 10, 2006 10:30 pm

Post by vivian22 » Wed Nov 15, 2006 4:40 pm

If anybody had same problem before, could you let me know the solution? I am stuck in the issue for several days.

Usually swing components can be dragged and dropped. But I couldn't find any chart I can drag and drop. So I manually made the chart and tried to put on a swing jPanel. Please see the code above.

vivian22
Posts: 13
Joined: Fri Nov 10, 2006 10:30 pm

Post by vivian22 » Wed Nov 15, 2006 4:46 pm

skunk wrote:As a test, what happens if you replace this line

Code: Select all

 jPanel1.add(chartPanel,CENTER_ALIGNMENT);
with this

Code: Select all

 jPanel1.add(new JButton("foo"),CENTER_ALIGNMENT);
If the button does not appear where you expect, then the problem is not in the chartPanel but somewhere else.
I replaced that line and no buttons showed. In the case of button, I can easily drag and drop a button on the panel. Then the code will be generate automatically in a method "private void initComponents()". But this method couldn't be edited manually. And also there is nothing I can drag and drop for the chart.

This really makes me headache.

forsey85
Posts: 17
Joined: Mon Jul 03, 2006 2:55 pm

Post by forsey85 » Wed Nov 15, 2006 4:53 pm

So it isn't a problem with your JFreeChart code, its your swing code.

What LayoutManager is the panel using? is CENTER_ALIGNMENT the correct value for the LayoutManager?

Are you adding the panel after or before you do

pack();
setVisible(true);

If you are putting the panel on afterwards, you can do a

revalidate();
repaint();

to display the panel.

vivian22
Posts: 13
Joined: Fri Nov 10, 2006 10:30 pm

Post by vivian22 » Wed Nov 15, 2006 10:46 pm

I got it work. The chart is display on the swing panel now. I changed that panel layout to absolute layout. Then add one line code as below:

jPanel24.add(chartPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 390, 250));

Then it works.

Thanks for everybody's help.

Locked