CardLayout with JFreeChart switching is not working
Asked Answered
F

2

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

Faucal answered 3/11, 2012 at 11:15 Comment(4)
Please edit your question to include an sscce that exhibits the problem you describe.Cuvette
pastebin.com/ia0iJNti -this is tabbed pane where I'm invoking switching, is that sscce now ?Faucal
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 JTabbedPaneEpps
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 displayedFaucal
G
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.

Gemina answered 3/11, 2012 at 11:59 Comment(2)
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.Faucal
Ok, I wasn't sure. But yes, you will need a separate chart object for each graph you want to show.Gemina
C
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);
    }
    ...
}
Cuvette answered 3/11, 2012 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.