# JFreeChart Diagramm in GUI, nicht in neuem Fenster anzeigen



## Sachse123 (16. Dez 2012)

Hallo,

ich versuche ein JFreeChart in einem JPanel zu öffnen. Bei meinem Code wird das Diagramm immer in einem neuen Fenster dargestellt.

Hab schon mehrere Sachen versucht, habe aber noch keine Lösung gefunden =(


Das hier ist die GUI, in die das Diagramm eingefügt werden soll:


```
package de.vogella.jfreechart.swing;
import java.awt.BorderLayout;

import javax.swing.*;

import org.jfree.ui.RefineryUtilities;

public class GridGUI extends javax.swing.JFrame{

	    public GridGUI() {
	        initComponents();
	    }

	    @SuppressWarnings("unchecked")

	    private void initComponents() {

	        jTabbedPane1 = new javax.swing.JTabbedPane();
	        jPanel1 = new javax.swing.JPanel();
	        jPanel2 = new javax.swing.JPanel();
	        jProgressBar2 = new javax.swing.JProgressBar();
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jProgressBar1 = new javax.swing.JProgressBar();
	        jLabel3 = new javax.swing.JLabel();
	        jLabel4 = new javax.swing.JLabel();
	        jPanel3 = new javax.swing.JPanel();
	        jComboBox2 = new javax.swing.JComboBox();
	        jPanel4 = new javax.swing.JPanel();
	        jPanel5 = new javax.swing.JPanel();
	        jPanel6 = new javax.swing.JPanel();
	        jPanel7 = new javax.swing.JPanel();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        jTabbedPane1.setToolTipText("");
	        jTabbedPane1.setName(""); 
	        jTabbedPane1.setOpaque(true);

	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	        jPanel1.setLayout(jPanel1Layout);
	        jPanel1Layout.setHorizontalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 683, Short.MAX_VALUE)
	        );
	        jPanel1Layout.setVerticalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 354, Short.MAX_VALUE)
	        );

	        jTabbedPane1.addTab("Rohdaten", jPanel1);

	        
	        final BarChartDemo5 demo = new BarChartDemo5("Minimal Chart Demo");
	        demo.pack();
	        RefineryUtilities.centerFrameOnScreen(demo);
	        demo.setVisible(true);
	        
	         //jPanel2.add(demo, BorderLayout.NORTH);
	         //jPanel2.validate();
	        
	        JPanel jPanel2 = new JPanel( new BorderLayout() );

	        jPanel2.setLayout( new BorderLayout( 2, 2 ) );

	        jTabbedPane1.addTab("Grundstatistiken", jPanel2);

	        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
	        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jComboBox2ActionPerformed(evt);
	            }
	        });

	        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
	        jPanel3.setLayout(jPanel3Layout);
	        jPanel3Layout.setHorizontalGroup(
	            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel3Layout.createSequentialGroup()
	                .addGap(21, 21, 21)
	                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(606, Short.MAX_VALUE))
	        );
	        jPanel3Layout.setVerticalGroup(
	            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel3Layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(323, Short.MAX_VALUE))
	        );

	        jTabbedPane1.addTab("Elementcharakteristik", jPanel3);

	        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
	        jPanel4.setLayout(jPanel4Layout);
	        jPanel4Layout.setHorizontalGroup(
	            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 683, Short.MAX_VALUE)
	        );
	        jPanel4Layout.setVerticalGroup(
	            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 354, Short.MAX_VALUE)
	        );

	        jTabbedPane1.addTab("Ähnlichkeitsmatrix Elemente", jPanel4);

	        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
	        jPanel5.setLayout(jPanel5Layout);
	        jPanel5Layout.setHorizontalGroup(
	            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 683, Short.MAX_VALUE)
	        );
	        jPanel5Layout.setVerticalGroup(
	            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 354, Short.MAX_VALUE)
	        );

	        jTabbedPane1.addTab("Ähnlichkeitsmatrix Konstrukte", jPanel5);

	        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
	        jPanel6.setLayout(jPanel6Layout);
	        jPanel6Layout.setHorizontalGroup(
	            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 683, Short.MAX_VALUE)
	        );
	        jPanel6Layout.setVerticalGroup(
	            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 354, Short.MAX_VALUE)
	        );

	        jTabbedPane1.addTab("Differenzmatrix Elemente", jPanel6);

	        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
	        jPanel7.setLayout(jPanel7Layout);
	        jPanel7Layout.setHorizontalGroup(
	            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 683, Short.MAX_VALUE)
	        );
	        jPanel7Layout.setVerticalGroup(
	            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 354, Short.MAX_VALUE)
	        );

	        jTabbedPane1.addTab("Differenzmatrix Konstrukte", jPanel7);

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
	                .addContainerGap())
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jTabbedPane1)
	        );

	        jTabbedPane1.getAccessibleContext().setAccessibleName("");

	        pack();
	    }


	    public static void main(String args[]) {
	        try {
	            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
	                if ("Nimbus".equals(info.getName())) {
	                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
	                    break;
	                }
	            }
	        } catch (ClassNotFoundException ex) {
	            java.util.logging.Logger.getLogger(GridGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (InstantiationException ex) {
	            java.util.logging.Logger.getLogger(GridGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (IllegalAccessException ex) {
	            java.util.logging.Logger.getLogger(GridGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
	            java.util.logging.Logger.getLogger(GridGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        }

	        
	        java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	                new GridGUI().setVisible(true);
	            }
	        });
	    }

	    private javax.swing.JComboBox jComboBox2;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JLabel jLabel2;
	    private javax.swing.JLabel jLabel3;
	    private javax.swing.JLabel jLabel4;
	    private javax.swing.JPanel jPanel1;
	    private javax.swing.JPanel jPanel2;
	    private javax.swing.JPanel jPanel3;
	    private javax.swing.JPanel jPanel4;
	    private javax.swing.JPanel jPanel5;
	    private javax.swing.JPanel jPanel6;
	    private javax.swing.JPanel jPanel7;
	    private javax.swing.JProgressBar jProgressBar1;
	    private javax.swing.JProgressBar jProgressBar2;
	    private javax.swing.JTabbedPane jTabbedPane1;
}
```



Das hier ist das Diagramm:

```
package de.vogella.jfreechart.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BarChartDemo5 extends ApplicationFrame {

    public BarChartDemo5(final String title) {

        super(title);

        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        ArrayList<String> elemente=new ArrayList<String>();
        elemente.add("Element 1");
        elemente.add("Element 2");
        elemente.add("Element 3");
        elemente.add("Element 4");
        elemente.add("Element 5");
        elemente.add("Element 6");
        elemente.add("Element 7");
        elemente.add("Element 8");
        elemente.add("Element 9");
        elemente.add("Element 10");
        
        
        for(int i=0;i<10; i++)
        {
        	dataset.addValue(83.0, "First", elemente.get(i));
        }

        final JFreeChart chart = ChartFactory.createBarChart( //chart zu chartPanel
            "Anzahl der extremen Bewertungen pro Element (in Prozent):",  // chart title
            "Category",             // domain axis label
            "Score (%)",            // range axis label
            dataset,                // data
            PlotOrientation.HORIZONTAL,
            false,                  // include legend
            true,
            false
        );
        

        chart.setBackgroundPaint(Color.white);    
        final CategoryPlot plot = chart.getCategoryPlot();   

        plot.setRangeGridlinesVisible(true);
        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLowerMargin(0.05);
        domainAxis.setUpperMargin(0.05);
        domainAxis.setVisible(true);
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setRange(0.0, 100.0);
        rangeAxis.setVisible(true);


        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }
}
```



Danke fürs Lesen und noch eine schöne Nacht =)


----------



## SlaterB (16. Dez 2012)

> final ChartPanel chartPanel = new ChartPanel(chart);

hier wird ein Panel erzeugt welches du in deine GUI einfügen kannst,
wenn du grundsätzlich weißt wie du überhaupt einen Button, ein Panel usw. verwenden kannst,

der Code sieht ja schlimm nach GUI-Designer aus, wie der dafür zu bedienen wäre, das weiß ich freilich nicht,
erst muss man die Kontrolle über den Code erlangen, allen anderen Programmen abschwören, dann kann man anfangen zu programmieren


----------



## Sachse123 (16. Dez 2012)

Ich habe es schon mit .add versucht, doch bei mir kam dann immer diese Exception:

java.lang.IllegalArgumentException: adding a window to a container


----------



## SlaterB (16. Dez 2012)

das BarChartDemo5-Objekt kannst du nicht adden, das ChartPanel muss es sein,
lege es in einem Instanzattribut ab und hole es per getter,


----------



## Sachse123 (17. Dez 2012)

Danke, werde ich mal ausprobieren =)


----------

