Adding chart into JPanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
moddinati
Posts: 2
Joined: Fri Jun 13, 2008 9:40 pm

Adding chart into JPanel

Post by moddinati » Mon Jun 23, 2008 10:56 pm

I'm trying to add a chart into an existing JPanel in Java. The following code will run and compile fine, but it gives me a page worth of errors.

here is the code I use to insert my ChartPanel into my JPanel

Code: Select all

JPanel tempPanel = new JPanel();
        //Clears out the JPanel
        panelINhistogram.removeAll();
        panelINhistogram.validate();
              
        //Check which graph is selected.
        if(CBall.isSelected())
            tempPanel = kdtree.panelAll;
        else if(CBmin.isSelected())
            tempPanel = kdtree.panelMin;
        else if (CBmax.isSelected())
            tempPanel = kdtree.panelMax;
        else if(CBave.isSelected())
            tempPanel = kdtree.panelAve; 
        

        
        //Displays Histogram into JPanel with proper formatting
        GroupLayout lm = (GroupLayout)panelINhistogram.getLayout();
        // If GroupLayout is already set
        // A GroupLayout is used by NetBeans GUI builder,
        // so if you built the application in NetBeans,
        // you are probably working with an
        // existing GroupLayout for your JPanels
        lm.setAutocreateContainerGaps(true);
        lm.setAutocreateGaps(true);
        GroupLayout.SequentialGroup hGroup = lm.createSequentialGroup();
        hGroup.add(tempPanel);
        lm.setHorizontalGroup(hGroup);
        GroupLayout.SequentialGroup vGroup = lm.createSequentialGroup();
        vGroup.add(tempPanel);
        lm.setVerticalGroup(vGroup);
        
        //Refreshes the JPanel
        tempPanel.revalidate();
the first couple error messages I get begin like

Code: Select all

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: org.jfree.chart.ChartPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=680,height=420]] is not attached to a horizontal group
        at org.jdesktop.layout.GroupLayout.checkComponents(GroupLayout.java:986)
        at org.jdesktop.layout.GroupLayout.prepare(GroupLayout.java:940)
        at org.jdesktop.layout.GroupLayout.minimumLayoutSize(GroupLayout.java:792)
        at java.awt.Container.minimumSize(Container.java:1598)
        at java.awt.Container.getMinimumSize(Container.java:1583)
        at javax.swing.JComponent.getMinimumSize(JComponent.java:1706)

...and so on.....
Any Ideas? I'm new to Java and jfreechart so any help is appreciated.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Tue Jun 24, 2008 6:40 am

Hi moddinati,
that has nothing to do with JFreeChart but with GroupLayout. I have no idea about GroupLayout so I can´t help you out here. Have you checked the docs for GroupLayout?
http://java.sun.com/javase/6/docs/api/j ... ayout.html

moddinati
Posts: 2
Joined: Fri Jun 13, 2008 9:40 pm

Post by moddinati » Tue Jun 24, 2008 6:11 pm

Thanks for the reply paradoxoff.

From what I read, if I understand it correctly, is that jfreechart uses the flowlayout, and I'm trying to put it in the GoupLayout. I thought my code would take care of that fine.

The one thing I noticed in the javadocs is that it uses the .addComponent method where I use the .add method. The only problem is, my system does not recognize the .addComponent method???

I guess this question would probably be best answered in the java forums. Thanks for the help anyway.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Tue Jun 24, 2008 9:14 pm

moddinati wrote: From what I read, if I understand it correctly, is that jfreechart uses the flowlayout, and I'm trying to put it in the GoupLayout. I thought my code would take care of that fine.
A JFreeChart is outside the AWT/Swing Component/Container/JComponent inheritance tree, so a JFreeChart doesn´t use any of the Java layouts. The ChartPanel, which can be used to draw the JFreeChart, extends JPanel which by default uses a FlowLayout.
What exactly you are doing with your ChartPanel is not clear since that piece of code is missing from your snippet.

prassoon
Posts: 14
Joined: Fri May 23, 2008 1:57 am

Post by prassoon » Fri Jun 27, 2008 12:41 am

Try adding like this

Code: Select all

GroupLayout layout = new GroupLayout(chartPanel);
                                    chartPanel.setLayout(layout);
                                    layout.setAutocreateGaps(true);
                                    layout.setAutocreateContainerGaps(true);
                                    layout.setHorizontalGroup((layout.createSequentialGroup()).add(jpanel));
                                    layout.setVerticalGroup(layout.createSequentialGroup().add(
                                            layout.createParallelGroup(GroupLayout.CENTER)).add(jpanel));

Locked