title: update a jfreechart
body: <p>I have a jfreechart which appears after the user clicks a button. When the user clicks the button again with a different dataset the graph should be repainted. My problem is that even the new graph is displayed once the user clicks the button for the second time, it disappears and the previous graph appears when the user click on anywhere else in the graph. </p>
  
  <p>Please help me on this.</p>
  
  <p>This is where the graph is created.</p>
  
  <pre><code> public JFreeChart createChart() throws FileNotFoundException, IOException {
  
       userAligner.userRecord();
       userAligner.diffVoiceText();
     // aligner.roggerRecord();
      //aligner.userRecord();
      final CategoryDataset dataset1 = aligner.roggerRecord();
      final NumberAxis rangeAxis1 = new NumberAxis("Pace - Rogger(ms)");
      rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
      final LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
      renderer1.setSeriesPaint(0, Color.blue); //int series, paint paint
      renderer1.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
      final CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1, renderer1);
      subplot1.setDomainGridlinesVisible(true);
  
      final CategoryDataset dataset2 = userAligner.createData();
      dataset2.addChangeListener(subplot1);
      final NumberAxis axis2 = new NumberAxis("Pace - User(ms)");
      subplot1.setRangeAxis(1, axis2); //value axis
      subplot1.setDataset(1, dataset2); //int index
      //subplot1.mapDatasetToRangeAxis(1, 1); // int index, int axisindex
      final CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
      renderer2.setSeriesPaint(0, Color.red);
  
      subplot1.setForegroundAlpha(0.7f);
      subplot1.setRenderer(0, renderer1);
      subplot1.setRenderer(1, renderer2);
  
  
  
      final CategoryAxis domainAxis = new CategoryAxis("Words");
     // domainAxis.setMaximumCategoryLabelLines(10);
      domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
      final CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(domainAxis);
  
  
      plot.add(subplot1, 1);
     // plot.add(subplot2, 1);
  
  
      final JFreeChart chart = new JFreeChart(
              "Pace Graph", new Font("SansSerif", Font.BOLD, 14),
              plot, true);
      return chart;
  }
  
  public void datasetChanged(DatasetChangeEvent arg0) {
      try {
          createChart();
      } catch (FileNotFoundException ex) {
          Logger.getLogger(PaceChart.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
          Logger.getLogger(PaceChart.class.getName()).log(Level.SEVERE, null, ex);
      }
  </code></pre>
  
  <p>}</p>
  
  <p>And it is updated every time with the action when the user clicks a button as below.</p>
  
  <pre><code>private void viewPaceButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
      // TODO add your handling code here:
      if (file == null){
           JOptionPane.showMessageDialog(VoiceTracker.f,"Save the Voice before View Pace Graph");
       }
      else {
      //jScrollPane2.setLayout(new java.awt.BorderLayout());
     // PaceChart pc = new PaceChart();
      PaceChart dac = new PaceChart();
      //SymbolAxisDemo1 demo = new SymbolAxisDemo1();
      ChartPanel CP;
          try {
              CP = new ChartPanel(dac.createChart());
              paceAnalyzePanel.add(CP, BorderLayout.CENTER);
              paceAnalyzePanel.validate();
  
          } catch (FileNotFoundException ex) {
              Logger.getLogger(VoiceTracker.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException ex) {
              Logger.getLogger(VoiceTracker.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  
  
  }      
  </code></pre>