How to add a chart to a specific panel?
How to add a chart to a specific panel?
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.
-
- 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?
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.
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.
Re: How to add a chart to a specific panel?
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.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: How to add a chart to a specific panel?
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.
You could try to set the preferred size and see whether that makes a difference.
Re: How to add a chart to a specific panel?
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?
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.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: How to add a chart to a specific panel?
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?
Re: How to add a chart to a specific panel?
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?
-
- 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?
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.leshem wrote: setContentPane(localJPanel);
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader

