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

2
  • 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. Commented 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. Commented Jul 24, 2013 at 12:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.