CardLayout with JFreeChart switching is not working

旧时模样 提交于 2019-12-13 04:25:35

问题


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


回答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.




回答2:


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);
    }
    ...
}


来源:https://stackoverflow.com/questions/13208626/cardlayout-with-jfreechart-switching-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!