I am new to java and JFreecharts and I am using Jfreechart to create many charts(barcharts and piecharts). I display the charts as and when it is created. But all the charts which are generated gets stacked upon the other and the last chart is on the top. If i close the last one all the charts gets closed. I want to know if it is possible to have only one frame and all the charts can be navigated using a 'next' and 'previous'button. If anyone has experience in this, please share. Thanks in Advance
-
Has less to do with JFreeChart, more with Swing. With JTabbedPane the user could elect immediately the pie chart as there is a tab "Pie." With previous+next you can have a panel that on button click sets the next/previous chart into the panel.Joop Eggen– Joop Eggen2013-07-24 12:16:03 +00:00Commented Jul 24, 2013 at 12:16
-
@JoopEggen: what i have is 6 functions, which calculate 6 different parameter and each one creates a Jfreechart(barchart, piechart, xylinechart). Each time I construct a panel that displays the specified jfreechart. Then, I set the size of the panel and then call setContentPane(panel) and sets the frame to visible.java_learner– java_learner2013-07-24 12:37:07 +00:00Commented Jul 24, 2013 at 12:37
Add a comment
|
2 Answers
The following is more to get you started. Better use a GUI editor like that of the NetBeans IDE. Mind the following is typed, without seeing a compiler.
Fields:
private static final int CHARTS = 6;
private int currentChartNo = 0;
private JButton previousButton = new JButton("<");
private JButton nextButton = new JButton(">");
private JPanel currentChartPanel = new JPanel();
private JPanel[] chartPanels = new JPanel[CHARTS];
Initialisation in the frame:
// getContentPane(), having per default a BorderLayout.
add(currentChartPanel , BorderLayout.CENTER);
add(previousButton, BorderLayout.WEST);
add(nextButton, BorderLayout.EAST);
previousButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (currentChartNo > 0) {
getContentPane().remove(currentChartPanel);
--currentChartNo;
currentChartPanel = chartPanels[currentChartNo];
getContentPane().add(currentChartPanel, BorderLayout.CENTER);
repaint(100L);
}
}
});
It can be made nicer, with an extra chart containing panel.
Comments
As an alternative to multiple instances of ChartPanel, you can create a series of datasets and update a single panel, as shown here.
