Java JFreeChart未更新

Java JFreeChart未更新,java,model-view-controller,dataset,jfreechart,Java,Model View Controller,Dataset,Jfreechart,一些背景信息:我正在创建一个系统来登记丢失、找回和归还的行李。这3个变量的数量需要在折线图中,以便从一段时间内丢失的行李数量等方面获得一个清晰的概览 现在的问题是: 创建折线图并将其添加到图表面板时,折线图表示数据集。我听说当您编辑/更新数据集时,图表也会自动更新。嗯,我有一个更新按钮旁边的图表,它必须更新图表时,点击。独立于已经存在的JPanel/ChartPanel中的图表,我还创建了一个表示ChartFrame的新框架 当我单击更新按钮时,会弹出一个新的框架,其中包含来自更新数据集的最新数

一些背景信息:我正在创建一个系统来登记丢失、找回和归还的行李。这3个变量的数量需要在
折线图中
,以便从一段时间内丢失的行李数量等方面获得一个清晰的概览

现在的问题是: 创建
折线图
并将其添加到
图表面板
时,
折线图
表示数据集。我听说当您编辑/更新数据集时,图表也会自动更新。嗯,我有一个更新按钮旁边的图表,它必须更新图表时,点击。独立于已经存在的
JPanel
/
ChartPanel
中的图表,我还创建了一个表示
ChartFrame
的新框架

当我单击更新按钮时,会弹出一个新的框架,其中包含来自更新数据集的最新数据。当我再次单击它时,显然会创建另一个框架,但是已经存在的框架也会更新,而
JPanel
中的图表则不会更新,尽管它们使用的是相同的数据集,即
static
,并且来自线形图模型

这里有一些代码:

线形图模型

//At the top of the document the dataset is initialize static
public static DefaultCategoryDataset dataset = null;

/*****************Other code omitted***********************/
/**
 * 
 * Updates an already existing dataset
 * 
 * @param dataset
 * @return dataset
 */
public CategoryDataset updateDataset(DefaultCategoryDataset dataset) {

    this.dataset = dataset; 

    // row keys...
    final String rowLost = "Lost";
    final String rowFound = "Found";
    final String rowReturned = "Returned";


    //Don't pay attention to this. It's setting the value for the dataset from different arrays which I know of the are filled correctly
    int i = 0;
    while (i < 12) {

        dataset.setValue(lost[i], rowLost, type[i]);
        System.out.println("There were " + lost[i]
                + " lost luggages in month: " + type[i]);
        i++;
    }
    for (int j = 0; j < 12; j++) {
        dataset.setValue(found[j], rowFound, type[j]);
        System.out.println("There were " + found[j]
                + " found luggages in month: " + type[j]);
    }
    for (int j = 0; j < 12; j++) {
        dataset.setValue(returned[j], rowReturned, type[j]);
        System.out.println("There were " + returned[j]
                + " returned luggages in month: " + type[j]);
    }
    return dataset;

}
ChartController

LineChartModel model = new LineChartModel();
public ChartPanel chartPanel;
/**
 * Creates a new demo.
 *
 * @param title  the frame title.
 */
public LineChart(final String title, LineChartModel m) {
    m = model;
    m.selectRange();
    m.createDataset();
    final JFreeChart chart = createChart(m.dataset);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(500, 270));
}
首先是构造函数的参数

public ChartController(final ManCharts v, final LineChartModel m) {
    view = v;
    model = m;
这里是按钮的actionlistener

    //Select a range by sumbitting the variables
    v.date.btnSubmit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            //selectRange select all the data between 2 dates but isn't important for this problem
            m.selectRange(/*v.date.dateChooserFrom, v.date.dateChooserTo*/);

            //updateDataset updates the dataset from the LineChartModel which is static
            m.updateDataset(m.dataset);

            //The data in the chart should already be updated but here I'm trying to replace the current chart by a new one
            v.chart.chartPanel = new ChartPanel(v.chart.createChart(m.dataset));

            //This is the new chart which does automatically update when the button is pressed
            JFreeChart chart = ChartFactory.createLineChart("Something chart", "Date", "Value", m.dataset);
            ChartFrame frame = new ChartFrame("New line chart", chart);
            frame.setVisible(true);
        }
    });
我真的希望有人能帮上忙,如果没有完整的代码,这个问题不会太复杂

如果你需要更多的代码什么的。就这么说吧

提前感谢

编辑 我认为这与我如何创建图表有关。在应用程序开始时创建的JPanel中的图表是使用编辑过的方法创建的(这是我的LineChart类的一部分,位于我的构造函数下方):

已解决 这与单击submit按钮时创建的另一个数据集有关。所以我已经修好了娱乐设施,现在开始工作了。这不是一个真正的图表问题,只是我的模型中的一个问题。无论如何,谢谢你的帮助

实现一个事件监听器,在需要时提示它重新绘制()<代码>JPanel没有。例如,请参见中的实现。调试时,请查找隐藏另一个实例的
ChartPanel
,或者查找错误使用
JPanel

Addednum框架是否也需要是
ApplicationFrame
,或者它是否可以在
JFrame
中工作


可以接受,如本
JFrame
或本
ApplicationFrame
所示;两者都是动态更新的。请注意,Swing GUI对象应仅在上构造和操作。

框架是否也需要使用
ChartFrame
或是否可以在
JFrame
中工作?顺便说一句,我认为这与我如何创建图表有关。我将在一秒钟内用更多的代码更新我的问题。
JFrame
可以很好地工作,例如;不要添加更多的代码片段,请编辑您的问题,以包含显示问题的;这样做通常有助于调试;另请参见。帖子已更新。请参见编辑!下的其他信息!。你的帮助已经很感激了!没有什么引人注目的;我编辑了答案以解决框架问题;请参阅我关于调试的更新注释。
/**
 * Creates a sample chart.
 * 
 * @param dataset  a dataset.
 * 
 * @return The chart.
 */
public JFreeChart createChart(final CategoryDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createLineChart(
        "Luggage",       // chart title
        "Time",                    // domain axis label
        "Value",                   // range axis label
        dataset,                   // data
        PlotOrientation.VERTICAL,  // orientation
        true,                      // include legend
        true,                      // tooltips
        false                      // urls
    );



    chart.setBackgroundPaint(Color.decode("#d6d9df"));

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.white);

    // customise the range axis...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(true);


    // customise the renderer...
    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    //renderer.setDrawShapes(true);

    renderer.setSeriesStroke(
        0, new BasicStroke(
            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] {10.0f, 6.0f}, 0.0f
        )
    );
    renderer.setSeriesStroke(
        1, new BasicStroke(
            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] {6.0f, 6.0f}, 0.0f
        )
    );
    renderer.setSeriesStroke(
        2, new BasicStroke(
            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] {2.0f, 6.0f}, 0.0f
        )
    );
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;
}