Results 1 to 5 of 5
  1. #1
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default problems displaying chart

    I'm using JFreeChart to create a a chart and then I create a ChartPanel to display the chart. The code is part of a class called MassSweep:

    Java Code:
    private PortHandler portHandler;
        private DefaultXYDataset data = new DefaultXYDataset();
        private JFreeChart graph = ChartFactory.createXYLineChart("Mass Sweep", 
                "Pressure (Torr)", "Mass (amu)", data, PlotOrientation.HORIZONTAL, 
                false, true, false);
    Later in another class which is a JFrame, I have a panel within the frame that I've already added to the frame. I add the ChartPanel to the panel which is on the frame. So I thought it would work to do this:
    Java Code:
    jPanel1.add(massSweep.display());
    But when I run my program, the chart does not display.

    This is the code generated automatically by NetBeans (the IDE). I don't get it...
    Java Code:
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 1045, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 361, Short.MAX_VALUE)
            );

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: problems displaying chart

    To start off are you able to display a background color in your JPanel? it's a good start...

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

    Default Re: problems displaying chart

    If you're going to be adding components on the fly at run-time, you'll need to have your containers (here your JPanel that is accepting the new component) use layout managers that can handle this better. If you haven't yet done so, I suggest that you read the layout manager tutorials in the Swing tutorials.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: problems displaying chart

    If you add/remove components to a container that is already realized (eg visible), you must revalidate or validate the container (this is clearly defined in the API for the add method of container - http://docs.oracle.com/javase/6/docs...t.Component%29 ). Try calling revalidate() pr validate() followed by repaint() on the JPanel you add the Chart to...but as Fubarable mentioned, there are perhaps other alternatives you can use to facilitate what you need (for instance, a CardLayout)
    Last edited by doWhile; 01-31-2012 at 06:54 AM.

  5. #5
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

Similar Threads

  1. Displaying Mouse Coordinate problems
    By Fortu in forum New To Java
    Replies: 3
    Last Post: 02-07-2011, 03:43 AM
  2. how to record movement and displaying in chart
    By dianulhaq in forum AWT / Swing
    Replies: 0
    Last Post: 02-06-2011, 04:17 AM
  3. JList displaying problems
    By D.Calladine in forum New To Java
    Replies: 0
    Last Post: 12-03-2010, 12:27 AM
  4. Problems displaying and array of JPanels
    By dousedfirepants in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 06:47 AM
  5. Problems with JPanels and displaying
    By Mastergeek666 in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2008, 01:32 AM

Posting Permissions

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