Basically I am trying to add the graph inside the Jpanel which already exists. I am getting the erorr "adding a window to a container"

Here is my code

 public void displayGraph(){

       String sql = "Select pressure, ventilage from statstbl";
DBCon conn = new DBCon();
       JDBCCategoryDataset dataset = new JDBCCategoryDataset(conn.connect(),sql);
       JFreeChart chart = ChartFactory.createLineChart("Chart", "Pressure", "Ventilage", dataset, PlotOrientation.VERTICAL, false, true, true);
       BarRenderer renderer = null;
       CategoryPlot plot = null;
       renderer = new BarRenderer();
       ChartFrame frame = new ChartFrame("Statistics",chart);
 jPanel3.add(frame, BorderLayout.CENTER);

   }catch(Exception ex){


I guess your ChartFrame is a kind of JFrame - ie a top-level container or window, so you cannot place that inside a lower-level container like JPanel.

If you are usimg JFreeChart then you can replace your ChartFrame with a ChartPanel. ChartPanel is a kind of JPanel, and can be added to another JPanel without any problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.