How to add a chart to a specific panel?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
leshem
Posts: 5
Joined: Sun Oct 18, 2009 7:35 am
antibot: No, of course not.

How to add a chart to a specific panel?

Post by leshem » Sun Oct 18, 2009 8:23 am

Hi, my name is leshem. I am learning jFreeChart recently. I tried very hard on add a chart to a specific panel in a frame. It always fails. I think my code is correct since I can show the chart in a new frame with the same code. But what I want is to show the chart in the same frame which I choose x and y from.Does anyone know how to do it? It's better to give an example code. It will help me a lot. Thank you.

leshem
Posts: 5
Joined: Sun Oct 18, 2009 7:35 am
antibot: No, of course not.

Re: How to add a chart to a specific panel?

Post by leshem » Sun Oct 18, 2009 10:18 pm

Anyone?

vindsr2211
Posts: 16
Joined: Mon Oct 19, 2009 1:40 am
antibot: No, of course not.

Re: How to add a chart to a specific panel?

Post by vindsr2211 » Mon Oct 19, 2009 1:53 am

If you are able to show it on another frame you must be able to show it on the previous frame also.

Make sure the components you add are to the same frame.

Before displaying the components of the second level of the frame, i.e. I mean your next frame, set the first frame to :

frame.setVisible(false);

& then..
start adding components to the frame.

leshem
Posts: 5
Joined: Sun Oct 18, 2009 7:35 am
antibot: No, of course not.

Re: How to add a chart to a specific panel?

Post by leshem » Mon Oct 19, 2009 7:02 am

I added the chartPanel to a jPanel which embeded in the current frame in order to show the chart. And I add code this.setVisible(false) before it. But when I run the program, the current frame disappear as I press the button and shows again with nothing on the frame. I use NetBeans to edit the interface, the jPanel is generated automatically after I drag it to the frame. I just don't understand why the chart doesn't show in the jPanel but works fine in a new frame. I debug it, system says "the variable information not available, source compiled without -g option". I'm suffering for a long time. Any new idea?
vindsr2211 wrote:If you are able to show it on another frame you must be able to show it on the previous frame also.

Make sure the components you add are to the same frame.

Before displaying the components of the second level of the frame, i.e. I mean your next frame, set the first frame to :

frame.setVisible(false);

& then..
start adding components to the frame.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How to add a chart to a specific panel?

Post by paradoxoff » Mon Oct 19, 2009 8:12 am

A ChartPanel doesn´t override getSize() or getPreferredSize(). SInce it doesn´t contain any child JComponents, it might be regarded as an "empty JPanel" by the LayoutManager and thus might be "drawn" with a zero size.
You could try to set the preferred size and see whether that makes a difference.

leshem
Posts: 5
Joined: Sun Oct 18, 2009 7:35 am
antibot: No, of course not.

Re: How to add a chart to a specific panel?

Post by leshem » Tue Oct 20, 2009 4:55 am

My code is

JPanel localJPanel = createDemoPanel();
localJPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(localJPanel);
scatterPanel.add(localJPanel);
scatterPanel.setVisible(true);

localJPanel is used to get the chartPanel generated by createDemoPanel(), I want to add localJPanel into scaterPanel which is a jPanel too but embeded in the current frame. It doesn't work.

But if I create a new class which I only need write two line of codes the chart will show up. Seeing below
JPanel localJPanel = createDemoPanel();
setContentPane(localJPanel);

I don't know why. Is there something wrong?

paradoxoff wrote:A ChartPanel doesn´t override getSize() or getPreferredSize(). SInce it doesn´t contain any child JComponents, it might be regarded as an "empty JPanel" by the LayoutManager and thus might be "drawn" with a zero size.
You could try to set the preferred size and see whether that makes a difference.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How to add a chart to a specific panel?

Post by paradoxoff » Tue Oct 20, 2009 7:21 am

What is the layout manager for scatterPanel? Have you called validate() and/or invalidate() on scatterPanel to indicate that it has to be laid out again and its size determined?

leshem
Posts: 5
Joined: Sun Oct 18, 2009 7:35 am
antibot: No, of course not.

Re: How to add a chart to a specific panel?

Post by leshem » Thu Oct 22, 2009 7:18 pm

Even I add scatterPanel.validate(), it doesn't show. But I found if I add a Internal Frame to the current frame to contain the chart panel, it shows. I still don't know how to let the chart shows in a panel.
paradoxoff wrote:What is the layout manager for scatterPanel? Have you called validate() and/or invalidate() on scatterPanel to indicate that it has to be laid out again and its size determined?

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

Re: How to add a chart to a specific panel?

Post by david.gilbert » Thu Oct 22, 2009 8:40 pm

leshem wrote: setContentPane(localJPanel);
Surely once you do that, the previous contents of your frame are lost. You don't give the context, so I don't know what you are calling setContentPane() on, so it is difficult to predict the result of this.
David Gilbert
JFreeChart Project Leader

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

Locked