How do I update a jPanel wth a Jfreechart

I'm trying to create a data file viewer with an application I'm writing. It works the first time, but if you try to press the button again and change the source file and view another data file, it processes it but does not change the graph. Suggestions?
private void jButtonSelectFileActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
        if (txtChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File f = txtChooser.getSelectedFile();
            configSettings.setDataFile(f.getName());
            jTextFieldODFDataFileLocation.setText(f.getAbsolutePath());
            dataStoragePath = f.getParent();
            configSettings.setDataStoragePath(dataStoragePath);
            final XYDataset dataset = createDataset(configSettings.getDataStoragePath()
                    +"\\"+configSettings.getDataFile());
            final JFreeChart chart = createChart(dataset);
            ChartPanel panel = new ChartPanel(chart);
            panel.setSize(jPanelChart.getWidth(),jPanelChart.getHeight());
            panel.setVisible(true);
            jPanelChart.add(panel);
            jPanelChart.validate();
            jPanelChart.repaint();
        }
    }

Open in new window

PmiDeveloperAsked:
Who is Participating?
 
objectsCommented:
u need to first remove the old one, and call revalidate()

            jPanelChart.removeAll();
            jPanelChart.add(panel);
            jPanelChart.revalidate();

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.