1

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

java_learner
  • 182
  • 3
  • 13
  • 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 Jul 24 '13 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 Jul 24 '13 at 12:37

2 Answers2

3

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
2

As an alternative to multiple instances of ChartPanel, you can create a series of datasets and update a single panel, as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045