Display JFreeChart inside a panel
Display JFreeChart inside a panel
I have JFrame form with several panels of user inputs. I would like to display a JFreeChart on a panel inside my JFrame. Is this possible?
>gr<
Well, as the name suggests, a ChartPanel is a Swing-Component that displays a chart in a Swing-Component.
If you need info on getting a swing-component displayed in an other swing component, then have a look at the Swing-Tutorials at java.sun.com, they give easy tutorials on how to write Swing-Applications.
http://java.sun.com/docs/books/tutorial/uiswing/
If you need info on getting a swing-component displayed in an other swing component, then have a look at the Swing-Tutorials at java.sun.com, they give easy tutorials on how to write Swing-Applications.
http://java.sun.com/docs/books/tutorial/uiswing/
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Take a look at org.jfree.chart.demo.PieChartDemo1.java.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
-
- Posts: 3
- Joined: Wed May 28, 2008 9:39 pm
ChartPanel does not appear in JFrame
I am also trying to display a chart in a JPanel; so far without luck. Reading the preceding posts and reviewing the JFreeChart documentation did not produce an answer. Using NetBeans 6.1, I created a simple 3 panel frame (header, side and main) and placed 3 buttons in the side panel. One button is a simple jPanelMain.add(label) that puts a new label in the main panel; this works fine. The second button creates a JFreeChart in a separate window using ChartFrame; this also works fine. BUT, many attempts to move the chart to the panel have failed. The program simply sits there running. I've tried a number of variations on the following basic code, including (but not limited to) UpdateUI, setPreferredSize and setContentPane. There must be a simple solution. Any help is appreciated.
ChartPanel chartpanel = new ChartPanel(chart);
jPanelMain.add(chartpanel);
return;
ChartPanel chartpanel = new ChartPanel(chart);
jPanelMain.add(chartpanel);
return;
ChartPanel does not appear in JFrame
The same problem. When chart is in separate window is ok. But when I want to put chart into JPanel (which is for example on the JFrame), it doesn't work correctly. The chart is dipslayed but not in JPanel but in whole window and all other components form window just dissappear ! Strange!
Re: ChartPanel does not appear in JFrame
Does it work as expected if you try jPanelMain.add(new JButton("foo"));harrierpark wrote: ChartPanel chartpanel = new ChartPanel(chart);
jPanelMain.add(chartpanel);
return;
If not, the problem is certainly related to the layout manager that is installed on the container
-
- Posts: 3
- Joined: Wed May 28, 2008 9:39 pm
Sounds like it might be a problem with the choice of layout manager.
Also....
When you do successive actions to add things to the jPanelMain, do you clear out any old components before adding the new one? It is possible, depending on the layout manager, that if older components are not removed first, the new component will be laid out in a way that is outside the range of the visible window. Also possible: if you haven't set the preferred size of the chart panel, some layout managers won't size your component properly - usually leaving it nothing more than a small dot or box.
Also....
When you do successive actions to add things to the jPanelMain, do you clear out any old components before adding the new one? It is possible, depending on the layout manager, that if older components are not removed first, the new component will be laid out in a way that is outside the range of the visible window. Also possible: if you haven't set the preferred size of the chart panel, some layout managers won't size your component properly - usually leaving it nothing more than a small dot or box.
If you read the source of org.jfree.chart.ChartPanel, you will find that the constructor sets the preferred size.jpmaia wrote:Also possible: if you haven't set the preferred size of the chart panel, some layout managers won't size your component properly - usually leaving it nothing more than a small dot or box.
Code: Select all
572 setPreferredSize(new Dimension(width, height));