How to insert a JFreeChart chart in a panel on a separate GUI?
<p>I want to put a chart inside a specific panel in a GUI using JFreeChart. I have 2 java files (one with the GUI, and another that creates the graph) and would like , if possible, to keep it this way.</p>

<p>In the main GUI I have a panel called panelGraph:</p>

<pre><code>    JPanel panelGraph = new JPanel();
    panelGraph.setBounds(220, 64, 329, 250);
    panelMain.add(panelGraph); //it is inside the main panel
    panelGraph.setLayout(null);
</code></pre>

<p>And I also have a button that triggers the appearance of the graph:</p>

<pre><code>    Button btnGetGraph = new JButton("Draw Graph");
    btnGetGraph.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            XYLineChart_AWT.runGraph("Cenas", "Titulo", "XLABEL", "YLABEL", panelGraph);

        }
    });
    btnGetGraph.setFont(new Font("Tahoma", Font.BOLD, 13));
    btnGetGraph.setBounds(323, 327, 128, 34);
    panelMain.add(btnGetGraph);
</code></pre>

<p>And as follows, is the java file that creates the graph:</p>

<pre><code>  public class XYLineChart_AWT extends JFrame {
  public XYLineChart_AWT( String applicationTitle, String chartTitle, String xLabel, String yLabel, JPanel panel) {

  JFreeChart xylineChart = ChartFactory.createXYLineChart(
     chartTitle ,
     xLabel ,
     yLabel ,
     createDataset() ,
     PlotOrientation.VERTICAL ,
     true , false , false);

  ChartPanel chartPanel = new ChartPanel( xylineChart );
  chartPanel.setPreferredSize( panel.getSize() );


  final XYPlot plot = xylineChart.getXYPlot( );
  plot.setBackgroundPaint(new Color(240, 240, 240));
  XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,false);
  renderer.setSeriesPaint( 0 , Color.BLACK );
  renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
  plot.setRenderer( renderer );
  setContentPane(chartPanel);
  panel.add(chartPanel);
  }

private XYDataset createDataset( ){

  final XYSeries seno = new XYSeries ("Sin");
  for(double i=0;i&lt;=1440;i++){
      double temp=Math.sin(i*((2*Math.PI)/640) + Math.PI) + 1;
      seno.add(i/60, temp);
  }


  final XYSeriesCollection dataset = new XYSeriesCollection( );          
  dataset.addSeries(seno);
  return dataset;
}

public static void runGraph(String appTitle, String chartTitle, String xLabel, String yLabel, JPanel panel) {
  XYLineChart_AWT chart = new XYLineChart_AWT(appTitle, chartTitle, xLabel, yLabel, panel);

  chart.pack();
  chart.setVisible(true);
  panel.setVisible(true);

 }
}
</code></pre>

<p>This creates the graph and puts it into the specified panel (that I send through the method runGraph()).
However, it creates a 2nd JFrame ( I know i made chart.setVisible(true) and I can just put it to false) that I don't want to be created outside of the panel I send.</p>

<p>Is there any way to implement this that does not create that extra jFrame?</p>

<p>P.S.: Another question: the setBackgroundPaint() method changes the back of where the graph is shown. However the parts where the title and legend are don't change. How can I change those parts? </p>

<p>Thank you for your help,</p>

<p>Nhekas</p>