Is it legal to add a graph to a JPanel?

Hi, I am working on a gui for one of my classes that needs to display a graph that shows the number of failed and passed questions on a test. My bar graph works perfectly when I test it separately, but when I try to implement it in the rest of my GUI, I get the following error:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
Here is the relevant code with this issue:
import javax.swing.JFrame;
public class TestStudentStatistics {
     public static void main(String[] args) {
          studentStatistics f = new studentStatistics();
          f.setTitle("Student History");
         f.setSize(1000,1000);
         f.setVisible(true);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public studentStatistics ()
          //adding the JPanels declared earlier in the code to a new container.
          Container c = this.getContentPane();
          c.add(jp1);
          c.add(jp2);
          c.add(jp3);
          c.add(jp4);
          c.add(jp5);
          /*creating an instance of the graph for the student's last three tests
           * This is currently designed for the prototype. These bars will be used in the
           * final product to show percentage failed overall per test and also per subject
           * area.
          bars graph = new bars(12,23,23,34,34,45);
          //adding the JList to the second JPanel
          jp2.add(jl1);
          //adding the graph to the third JPanel
          jp3.add(graph);
          //adding the Exit JButton declared earlier to the fourth JPanel
          jp4.add(jbtExit);
import java.awt.*;
import javax.swing.*;
public class bars extends JFrame
     private int true1, true2, true3, false1, false2, false3;
     public bars (double b1, double t1, double b2, double t2, double b3, double t3)
          true1 = (int)(b1/t1 * 300);
          false1 = 300-true1;
     public void paint(Graphics g2)
          double pass1;
          double fail1;
          fail1 = (double)false1/300*100;
          fail1 = Math.round(fail1);
          pass1 = (double)true1/300*100;
          pass1 = Math.round(pass1);
          //Drawing the first bar graph
          g2.setColor(Color.GREEN);
          g2.fillRect(10, 100, true1, 40);
          g2.setColor(Color.RED);
          g2.fillRect(true1+10, 100, false1, 40);
          g2.setColor(Color.BLACK);
          //true1 + false 1 is assumed to equal 300.
          g2.drawRect(10, 100, true1 + false1, 40);
          //Drawing the legend of the incorrect percentage to the right of the graph
          g2.setColor(Color.red);
         g2.fillRect(315,104,40,12);
         g2.drawString("Incorrect % = " + fail1,360,116);
         //Drawing the legend of the correct percentage to the right of the graph
         g2.setColor(Color.green);
         g2.fillRect(315,124,40,12);
         g2.drawString("Correct % = " + pass1,360,136);
         //Title of the first graph
         g2.setColor(Color.WHITE);
         g2.drawString(" TEST ONE ", 155, 120);
}do I have to add my graph in some other manner or is it impossible? I'm not quite sure what else I could do to implement my graph and let it still be within my gui.

jbjohnson86 wrote:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a containerYou can't add window(JFrame) into a container(JPanel) but a JPanel can have nested panel extend your bar into a JPanel and add that panel to a JFrame. It should be container to a window not window to a container.
In the future Swing related question should be posted in the Swing forum.
http://forum.java.sun.com/forum.jspa?forumID=57

Similar Messages

  • Is it possible to add a Graph in the Teststand Labview OI

    Is it possible to add a graph to the Labview OI so that data can be passed up via the sequence context? Would posting a UI Message be the right way to trigger the graph to update? I'm not sure how to do this since the graph is not a UI control.
    Has anyone done this or is better to just pop-up a graph in a sequence step?  

    paulmw,
    I have not tried this, but one way you could do this is by running the VI with the same version of the LabVIEW run-time engine that the OI was build with.  The LV OI will load the run-time engine initially.  When TestStand runs the test VI, it will try to first load the run-time engine specified in the adapter configuration.  If the specified version is the same as the version used by the LV OI, it will used the one in memory.  Since TestStand and the OI share the same run-time engine, creating a queue in the OI and populating with the VI run by TestStand will theorically work.  Like I said, I have not tried this, but I am assuming it will work.   The key is to make sure the LV version used to create the LV OI executable and the VI are the same.
    Jose

  • Add multiple graphs in one chart(ex.scatter chart) using labview excel report generation toolkit

    How to add multiple graphs in one chart(ex.scatter chart) using labview excel report generation toolkit. Assume like my test data is like below:
    X-Axis
    Data1
    Data2
    1
    1
    2
    13
    2
    3
    14
    3
    4
    15
    5
    5
    16
    3
    6
    7
    7
    The above graph generated manually in excel. I want to generate the same using excel report generation toolkit. when I use Excel Insert Graph.vi where the input data is 2D array with numeric data but here labview converts the empty cells (eg.x -axis 1,data1- is empty) as zero and while plotting i am getting zero for empty cell values.
    Can anyone please help me on how to plot the graph without adding zero for empty cells?
    Thanks&Regards,
    Bharathi T
    TE
    X-Axis
    Data1
    Data2
    1
    1
    2
    13
    2
    3
    14
    3
    4
    15
    5
    5
    16
    3
    6
    7
    7

    Hi Bharathi,
    The Excel Insert graph function takes in only 2d arrays as inputs. Since arrays can only have numeric values, any table with a null string is read as 0. Now to get a plot like you mentioned, you could read the table and replace blank Strings as NaN. NaN values in an array when plotted do not appear in the excel sheet. For reference I have attached a sample vi to help you understand.
    Regards
    Vijetha Nuthakki,
    Applications Engineer
    NI Systems India
    Attachments:
    Excel_graph_multiplot.vi ‏21 KB

  • How do you add a graph to a TDMS file

    Hello All,
    is it possible to add a graph or an image of my front panel graph to a TDMS log and if so, how would i do that?
    Thank you,

    As others have said TDMS is not the right file format for images.  It can be done but other options are probably better.  But that doesn't mean you can't start with TDMS and end with something like XLSX with images.  Here is a link for a tool I made that converts TDMS to XLSX, from there you can use ActiveX to import images and save them.  Or make real graphs like in Excel.  Or use the Report Generation Toolkit to edit the XLSX file after the conversion.
    https://decibel.ni.com/content/docs/DOC-36555

  • Oracle 10g forms developer, How to add a graph item

    Hi guys,
    We are the final stage of Oracle EBS R12 implementation and developing inhouse applications and deploying them right now. However, would appreciate if somebody would share the knowledge about adding graph items with form modules. Most of the google search provides pointers towards graphs for reports. Any help would be appreciated.
    We don't have existing graphs, so migrating from 6i is not a piont.
    Thanks and regards,

    Thank you very much Andreas. As you could read with my first post, we were totally into forms 6i until last few months and forms 10g is pretty new environment for us. We would appreciate incase if you could be more specific. Our requirement is simple right now, add a graph item. Hence, you may point us towards some repositories which are reliable for beginners.
    Thanks and regards,

  • How to add an image to a JPanel ?

    hi,
    do you now how to add an image to a JPanel ?
    thanks a lot !

    You can either use the Graphics method drawImage from the panel's paintComponent(Graphics g) method, or you can create an ImageIcon, with your Image in its constructor. And then create a JLabel, passing that ImageIcon in its constructor. Then, you can simple use the panel.add() method to add that JLabel.
    For using the paintComponent method, check out the thread already posted above (I'll type it in again just in case)
    http://forum.java.sun.com/thread.jsp?forum=31&thread=288769
    If you want to use a JLabel, you can do something like this:
    Image img;
    JLabel label = new JLabel(new ImageIcon(img));
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(label);Val

  • How to add an image in a JPanel

    Hi All,
    How to add an image in a JPanel and make it display.
    Thanks,

    I have tried with the below code. If I there is any fault please correct me.
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class HomePage extends JFrame implements     ActionListener {
        JButton cmdClick;
        JLabel label;
        JPanel homePanel = new JPanel();
        JPanel headPanel = new JPanel();
        JPanel btPanel = new JPanel();
        private JPanel mainPanel = new JPanel(new CardLayout());
        CardLayout cl;
        CalScenario calcFrame = null;
        public HomePage() {
           setTitle("Test Kit");
           setSize( 1008,399);
           setBackground( Color.gray );
           setResizable(false);
           Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
           Rectangle window = getBounds();
           setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
           setVisible(true);
            homePanel.setLayout(new BorderLayout());
            headPanel.setPreferredSize(new Dimension(1008,153));
            label = new JLabel("Main menu");
            headPanel.add(label);
            headPanel.setBackground(Color.CYAN);
            ImageIcon icon = new ImageIcon("images/slash.gif");
            JLabel imglabel = new JLabel();
            imglabel.setIcon(icon);
            headPanel.add(label);
            this.getContentPane().add(headPanel);
            btPanel.setBackground(Color.ORANGE);
            cmdClick = new JButton("Click here");
            btPanel.add(cmdClick);
            cmdClick.addActionListener(this);
            homePanel.add("North",headPanel);
            homePanel.add("West",btPanel);
            calcFrame = new CalScenario(mainPanel);
            mainPanel.add(homePanel, "HomePanel");
            mainPanel.add(calcFrame, "CalcFrame");
            cl = (CardLayout) (mainPanel.getLayout());
            add(mainPanel);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        public void actionPerformed(ActionEvent source)  {
          if (source.getSource() == (JButton) cmdClick) {
                cl.show(mainPanel, "CalcFrame");
        public static void main( String args[]) {
             HomePage homeFrame = new HomePage();
             homeFrame.setVisible(true);
    }

  • What code can i use to add an image to a JPanel?

    I tried multiple image tutorials and none of those supplied me with a way to add an image to a JPanel without using hidious code. Don't anyone know simpler code which i can use?

    Don't anyone know simpler code which i can use?Simpler than what? Post what you've got at the moment. Use code
    tags as described here: http://forum.java.sun.com/help.jspa?sec=formatting
    About the simplest I can think of is to make a JLabel using the
    constructor that takes an Icon argument, and then add it to the JPanel.
    (The Swing forum is the best place for this.)

  • Is it legal to add music from your own library onto someone elses IPOD????

    My friend wanted me to add some of my music onto his IPOD and the link for his IPOD will not show up in my ITunes. I do not know if it is legal to do this and perhaps this is preventing me from transferring the music. He has used the IPOD on his own computer before...

    Probably has nothing to do with your technical problem, but it is illegal for you to put your music on his iPod.
    Please don't steal music (or help him do so).

  • Can we add bar graph in smrtform at runtime?

    Hi,
    I am trying put bar graph inside smartform. The percentage/input i will pass at runtime and accordingly the graph will reflect the changes.
    CALL FUNCTION 'GRAPH_MATRIX_3D'
       EXPORTING
         COL1    = '2001'
         COL2    = '2002'
         COL3    = '2003'
         COL4    = '2004'
         TITL    = 'Expenses - India(INR).'
       TABLES
         DATA    = ITAB_MAIN
         OPTS    = ITAB_OPTIONS
       EXCEPTIONS
         OTHERS   = 1.
    With this FM i can print bar graph using report program. But I have to save this bar graph on local m/c then pass it to smartform.
    So please suggest me how to do it.
    thanks.

    I wonder if this http://download.oracle.com/docs/cd/E13159_01/osb/docs10gr3/javadoc/com/bea/wli/sb/management/configuration/ALSBConfigurationMBean.html
    can be of any help?

  • How to add a JScrollPane in a JPanel

    I have a JPanel (layout = null, size = 200*400).
    I would like to add a JScrollPane, that sizes 100*100 and that contains an other JPanel, at the location 0,200 in the first JPanel. I would like too that the JScrollBar is always visible.
    How is it possible ?

    The scrollbars will appear automatically when the preferred size of the panel is greater than the size of the scroll pane. So you probably need to add:
    panel.setPreferredSize(...);
    Of course if you use LayoutManagers, instead of a null layout, then this is done automatically for you.
    If you want the scroll bars to appear all the time then read the JScrollPane API.

  • How to add an image to the JPanel

    i have been searching for many to add in an iamge to the JPanel...but with error..can anyone kndly help?
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    public class adv extends JPanel {
    ImageIcon pic = new ImageIcon("home.gif");
    public void paintComponent(Graphics g) {
    g.drawImage(pic,0,0,this);
    super.paintComponent(g);
    public static void main(String[] args) {
    adv mpg= new adv();
    mpg.setLayout(new GridLayout(5,1,15,15));
         JFrame window = new JFrame ("dv");
    window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);     
    window.add(mpg);
    window.setVisible(true);
    window.setSize (550,225);
    }

    i have try out the suggestion that u have provided...still with error...wat can i do to solve it? any other way?
    below is how i add to my program...
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    public class adv extends JPanel {
        ImageIcon pic = createImageIcon("home.gif");
        public void paintComponent(Graphics g) {
      super.paintComponent(g);
    g.drawImage(pic.getImage(),0,0,this);
        public static void main(String[] args) {
           /*adv mpg= new adv();
          mpg.setLayout(new GridLayout(5,1,15,15));
             JFrame window = new JFrame ("dv");
             window.setLayout(new BorderLayout());
             frame.add(mpg, BorderLayout.CENTER);
           window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);     
           window.add(mpg);
           window.setVisible(true);
           window.setSize (550,225);*/
           adv mpg = new adv();
           JFrame frame = new JFrame();
         frame.setLayout(new BorderLayout());
         frame.add(pic, BorderLayout.CENTER);
         frame.setSize(500, 500);
         frame.setVisible(true);
    }

  • Trying to add an icon to a JPanel!

    After declaring the icon w/ :
    ImageIcon icon = new ImageIcon("logo2.gif");
    and the JPanel is added to the content pane, the error I am recieving is w/ this peice of code: jPanel1.add(icon);
    The error is: cannot reslove symbol, jPanel1.add(icon);
    ^
    I'm nit sure what the deal is...

    Here is some working code.You just have to center and resize the image.
    JFrame frame = new JFrame();
        frame.setSize(800,600);
        JLabel jLabel1 = new javax.swing.JLabel();
        JPanel jPanel1 = new javax.swing.JPanel();
        frame.getContentPane().setLayout(new javax.swing.BoxLayout(frame.getContentPane(), javax.swing.BoxLayout.X_AXIS));
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent evt) {
        jLabel1.setText("DMV Icon");
        ImageIcon icon = new ImageIcon("logo2.gif");
        frame.getContentPane().add(jLabel1);
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setMaximumSize(new java.awt.Dimension(120, 90));
        jPanel1.setMinimumSize(new java.awt.Dimension(120, 90));
        jPanel1.setPreferredSize(new java.awt.Dimension(120, 90));
        JLabel l = new JLabel();
        l.setIcon(icon);
        jPanel1.add(l);
        jPanel1.setBackground(Color.red);
        frame.getContentPane().add(jPanel1);
        frame.show();Hope this helps.

  • Is it possible to add an Actionlistener to a JPanel?

    Hi
    Is it possible to add a listener to a panel that would react when the mouse button was pressed whilst over the panel.
    I have panels displying data and when the user clicks on th panel I wish to expand the data for better viewing.
    Mickp

    Do I add the mouselistener to the panel itself or do I add it to the Frame and have to determine that the mouse is over the panel?
    MIckp

  • Interactive graphs in Edge Animate - how to add the jqPlot library

    Hello all
    I'd like to add interactive graphs in my Edge project, but can't use SVG. I'm now trying to implement the jqPlot library, but can't get it to work. Here is what I have so far:
    In "creationComplete" in "Stage" I've added this:
    $('<div id="chartdiv" style="height:400px;width:300px; "></div>').appendTo("#Stage");
    (function(){
      var plot1 = $.jqplot ('chartdiv', [[3,7,9,1,4,6,8,2,5]]);
    Nothing happens, and I'm not sure how to move on, any help would be greatly appreciated.

    Thank you, Raffalaff. From what I can tell it looks like jqPlot has a few more charts to choose among than Flot, otherwise from that they look very similar.
    To get jqPlot working I did this:
    $('<div id="jq_chart"></div>').appendTo(".chart");
    yepnope({
        nope:[
            'js/jquery.jqplot.min.js',
        complete: init
    function init(){
              $.jqplot('jq_chart', [ [[0, 0], [1, 1],[2,4],[50,2]] ], { yaxis: { max: 100 } });
    Would be great if I could set the div with class .chart to id #chart instead, but don't know how to do that. So until then, class will have to do.
    Thanks again, Raffalaff for helping me solve my problem :-)

Maybe you are looking for