title: Show JfreeChart on Button Click in swing
body: <p>So below is my code I have created the histogram chart using JFree I wanted to show the out put on Button click </p>
  
  <p>this is the histogram of normalized image </p>
  
  <p>i wanted to do that the on botton click the jfreechart is open on the panel and save in Drive</p>
  
  <p>please help me out I am newbie </p>
  
  <p>code is below..........</p>
  
  <pre><code>package org.ivb.jfreechart.barchart;
  
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  
  import javax.imageio.ImageIO;
  
  import org.jfree.chart.ChartFactory;
  import org.jfree.chart.ChartPanel;
  import org.jfree.chart.ChartUtilities;
  import org.jfree.chart.JFreeChart;
  import org.jfree.chart.plot.PlotOrientation;
  import org.jfree.chart.plot.XYPlot;
  import org.jfree.data.xy.IntervalXYDataset;
  import org.jfree.data.xy.XYSeries;
  import org.jfree.data.xy.XYSeriesCollection;
  import org.jfree.ui.ApplicationFrame;
  import org.jfree.ui.RefineryUtilities;
  
  public class Histogram extends ApplicationFrame {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      public Histogram(final String title) throws IOException {
          super(title);
          IntervalXYDataset dataset = createDataset();
          JFreeChart chart = createChart(dataset);
          final ChartPanel chartPanel = new ChartPanel(chart);
          chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
          setContentPane(chartPanel);
      }
  
      private IntervalXYDataset createDataset() throws IOException {
          BufferedImage imageA = ImageIO.read(new File("D://oriented.jpg"));
          int[] red = new int[imageA.getHeight() * imageA.getWidth()];
          int[] redhisto = new int[256];
          int[] pixel;
          int k = 0;
          for (int y = 0; y &lt; imageA.getHeight(); y++) {
              for (int x = 0; x &lt; imageA.getWidth(); x++) {
                  pixel = imageA.getRaster().getPixel(x, y, new int[3]);
                  red[k] = pixel[0];
                  k++;
              }
          }
  
          for (int x = 0; x &lt; red.length; x++) {
              int y = red[x];
              redhisto[y]++;
          }
  
          final XYSeries series = new XYSeries("No of pixels");
          for (int i = 0; i &lt; redhisto.length; i++)
              series.add(i, redhisto[i]);
  
          final XYSeriesCollection dataset = new XYSeriesCollection(series);
          return dataset;
      }
  
      private JFreeChart createChart(IntervalXYDataset dataset) {
          final JFreeChart chart = ChartFactory.createXYBarChart(
                  "Histogram of oriented Image", "X", false, "Y", dataset,
                  PlotOrientation.VERTICAL, true, true, false);
          XYPlot plot = (XYPlot) chart.getPlot();
          saveChart(chart);
          return chart;
  
      }
  
      public void saveChart(JFreeChart chart) {
          String fileName = "D:/histogram.jpg";
  
          try {
              ChartUtilities.saveChartAsJPEG(new File(fileName), chart, 800, 600);
          } catch (IOException e) {
              e.printStackTrace();
              System.err.println("creating erroe during chart prepartatin");
          }
  
      }
  
      public static void main(final String[] args) throws IOException {
  
          final Histogram demo = new Histogram("Histogram");
          demo.pack();
  
          RefineryUtilities.centerFrameOnScreen(demo);
          demo.setVisible(true);
  
      }
  
  }}
  </code></pre>