0

I encounter some problems with JFreeChart, here I explained what I'm creating:Random errors when changing series using JFreeChart. But now I have another related question. I have to ChartPanel in CardLayout to switch between the graphs when I click on tabbed pane. I've tried it with ordinary JPanel (public class JPaintablePanel extends JPanel. It's showing some button with different name depending on the tab), and it works well. But the same thing with public class JPaintablePanel extends ChartPanel is not working, it shows only one graph. Can you tell me how to force ChartPanel to switch, and preserve data?

http://pastebin.com/THuvGan5 ChartPanel

http://pastebin.com/Br2swZiC CardLayout

Community
  • 1
  • 1
aragornsql
  • 121
  • 1
  • 6
  • Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Nov 03 '12 at 11:24
  • http://pastebin.com/ia0iJNti -this is tabbed pane where I'm invoking switching, is that sscce now ? – aragornsql Nov 03 '12 at 11:29
  • 1
    No, that is not an SSCCE. And what an ugly extension of `JTabbedPane` is that. The idea behind a `JTabbedPane` is that you have a component on each tab, and you switch between these components by clicking on the tabs. Not sure what you try to do with that `CardLayout` inside a `JTabbedPane` – Robin Nov 03 '12 at 11:55
  • I didn't know how to do it in a different way. Each tab shows 'form' where user can supply parameters. With each tab switched there is jpanel and chartpanel that is displayed – aragornsql Nov 03 '12 at 12:03

2 Answers2

1

You don't need to mix tabbed panes and card layouts. Just put a separate ChartPanel in each tab.

JTabbedPane tabs = new JTabbedPane();
tabs.add("Graph 1", new JPrintablePanel());
tabs.add("Graph 2", new JPrintablePanel());

You should not have to write any code to deal with switching tabs, Swing will handle that for you. ChartPanels will also update automatically if you add data.

AngerClown
  • 6,149
  • 1
  • 25
  • 28
  • Yes, but my chartPanel and additional JPanel are not embedded in JtabbedPane. I'm sorry didn't make it clear JTabbedPane,ChartPanel,JPanel lays on the same grid, chart/jpanel are not part of TabbedPane, maybe that is wrong but that's how it is. Ass far as the single chart concerned I've found what I did, chart is staticaly initialized only once so it is displayed on only one chart. – aragornsql Nov 03 '12 at 12:10
  • Ok, I wasn't sure. But yes, you will need a separate chart object for each graph you want to show. – AngerClown Nov 03 '12 at 12:24
1

I'm sorry didn't make it clear: JTabbedPane, ChartPanel and JPanel lay on the same grid.

It's not clear from your question, but it may help to let the containment hierarchy reflect the intended usage. If each card is meant to enclose three panels, let each CardPanel contain three corresponding fields. Pass any required parameters in the CardPanel constructor. Add instances of these cards to the CardLayout, as shown in the examples found here and here. Use the strategy pattern to give individual cards a particular implementation of a common interface.

public class CardPanel extends JPanel {

    private JTabbedPane tabPane;
    private ChartPanel chart;
    private JPanel panel;

    public CardPanel(Dataset dataset, Context context, ...) {
        super(new GridLayout(0, 1));
        // initialize fields ...
        this.add(tabs);
        this.add(chart);
        this.add(panel);
    }
    ...
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045