Results 1 to 6 of 6
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post [SOLVED] Little help on adding a jfreechart graph to Jpanel

    Have been trying to add a Chartpanel to jpanel and its not working for me.
    Anyone with an idea on how to go about it?
    here is my code
    Java Code:
    package myjfreechart;
    
    import java.io.*;
    import java.util.StringTokenizer;
    import javax.swing.JPanel;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartFrame;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    /**
     *
     * @author  MUNYA00M
     */
    public class XYGraph extends javax.swing.JFrame {
    
      
    public XYGraph() {
    final XYSeries series = new XYSeries("Xenon Poisoning");
    
    //Read data from a given file 
    try {
           String aLine;
           int ro = 0;
           int col;
           String t, p;
           FileInputStream fis = new FileInputStream("myXenon.txt");
           // DataInputStream in = new DataInputStreasm(fis);
           BufferedReader br = new BufferedReader(new InputStreamReader(fis));
           
    //Read each line of the file
           while ((aLine = br.readLine()) !=null) {
               
    // Tokenize line into field values
           StringTokenizer st =   new StringTokenizer(aLine, " ");
           
           if (st.countTokens() >= 2){
           t= st.nextToken();
           double time = Double.parseDouble(t);
           p= st.nextToken();
           double power = Double.parseDouble(p);
        //   } else {
         //  break;
         //     }
         series.add(time, power);
           }
         }
           
        br.close();
        fis.close();   
     }catch (IOException e) {
    e.printStackTrace();   
        }      
    //series.add(0, 400.2);
    //series.add(1.0, 294.1);
    //series.add(2.0,350.0);
    //series.add(5,20);
    final XYSeriesCollection dataset = new XYSeriesCollection(series);
    
    JFreeChart chart = null;
    
    chart  = ChartFactory.createXYLineChart("Xenon Concentration and Poisoning",
            "Time(Hrs)", "Power Level(%)",(XYSeriesCollection) dataset, PlotOrientation.VERTICAL, true, true, false);
    
    // create and display a frame...
    ChartPanel CP = new ChartPanel(chart);
    //CP.pack();
    //CP.setVisible(true);
        
        /** Creates new form XYGraph */
        
            initComponents();  
            jPanel1.add(CP);
            jPanel1.validate();
        }
    
                        
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new XYGraph().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JPanel jPanel1;
        // End of variables declaration                   
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    what layout manager does your JPanel use?

    If you're not sure, you may wish to look into it, and even set the layout to BorderLayout. Then add the ChartPanel to the JPanel in the BorderLayout.CENTER position:

    Java Code:
    jPanel1.add(CP, BorderLayout.CENTER);
    I strongly recommend that you read the Sun Swing tutorials -- all of them, but especially the section on layout managers. Best of luck.
    Last edited by Fubarable; 02-24-2009 at 07:43 PM.

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    what layout manager does your JPanel use?

    If you're not sure, you may wish to look into it, and even set the layout to BorderLayout. Then add the ChartPanel to the JPanel in the BorderLayout.CENTER position:

    Java Code:
    jPanel1.add(CP, BorderLayout.CENTER);
    I strongly recommend that you read the Sun Swing tutorials -- all of them, but especially the section on layout managers. Best of luck.
    Hi,thanks alot for your reply.Am using Netbeans Ide as my builder.
    Have read the tutorial but somehow its not working out for me.
    I have also tried to implement the example above,BorderLayout but its not workin either.
    Am so confused now

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For the example above to work, you first must set the layout of your jPanel1 to BorderLayout which you can do via the NetBeans IDE. I can't tell by your post, but have you done this yet, and does it still not work? Can you post a picture of your GUI (even though it will be without the graph)?

  5. #5
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Thanks alot for ur help.I set jPanel to BorderLayout and it worked for me
    For anyone interested in the topic this is how i did it

    Java Code:
    JPanel jPanel1 = new JPanel();
    jPanel1.setLayout(new java.awt.BorderLayout());
    ........
    ChartPanel CP = new ChartPanel(chart);
    .....
    jPanel1.add(CP,BorderLayout.CENTER);
            jPanel1.validate();
    Thanks for your help

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome. My next recommendation is that you avoid using NetBeans to generate your Swing code and learn to do it by hand. It's the only way to truly learn Swing.

Similar Threads

  1. adding a jpanel in the middle of the script
    By 2o2 in forum AWT / Swing
    Replies: 11
    Last Post: 10-12-2008, 06:50 PM
  2. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 06:01 AM
  3. JFreeChart 1.0.8
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 09:27 PM
  4. JFreeChart 1.0.7
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-17-2007, 03:13 PM
  5. JFreeChart 1.0.5
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-10-2007, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •