Cannot show a chart on a Swing JPanel
Cannot show a chart on a Swing JPanel
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.
//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.
As a test, what happens if you replace this line
with this
If the button does not appear where you expect, then the problem is not in the chartPanel but somewhere else.
Code: Select all
jPanel1.add(chartPanel,CENTER_ALIGNMENT);
Code: Select all
jPanel1.add(new JButton("foo"),CENTER_ALIGNMENT);
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.
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.
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.skunk wrote:As a test, what happens if you replace this linewith thisCode: Select all
jPanel1.add(chartPanel,CENTER_ALIGNMENT);
If the button does not appear where you expect, then the problem is not in the chartPanel but somewhere else.Code: Select all
jPanel1.add(new JButton("foo"),CENTER_ALIGNMENT);
This really makes me headache.
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.
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.