Can't make it visible!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jannisery
Posts: 3
Joined: Thu Nov 09, 2006 7:55 pm

Can't make it visible!

Post by jannisery » Fri Mar 30, 2007 1:43 pm

Hi everyone ,

i have an applet with user interface and in the applet i have a JPanel name panel1... I init the dataset and in a function like DrawChart i called the dataset and try to draw the chart , like :

Code: Select all

public void DrawChart() {
      initDataSet();
      // create the chart...
      JFreeChart chart = ChartFactory.createBarChart(
            "Bar Chart", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
      );
      chartPanel = new ChartPanel(chart);
      chartPanel.setPreferredSize(new Dimension(panel1.getHeight(),panel1.getWidth()));
      panel1.add(chartPanel);
      JOptionPane.showMessageDialog( null, ""+panel1.getComponentCount());
      panel1.validate();
      panel1.repaint();

   }
and in my initComponents() function i call the DrawChart and as an applet i don't have main and i don't have frame - instead i use jpanel- ... Anything i've done i couldn't make the chart visible...

Can anyone help me?

Can't we use jpanel to contain jfreechart?

jannisery
Posts: 3
Joined: Thu Nov 09, 2006 7:55 pm

Post by jannisery » Fri Mar 30, 2007 2:29 pm

anyone? :( it's really urgent...

mentalsandcastles
Posts: 13
Joined: Fri Feb 09, 2007 12:39 am
Location: California, USA

Post by mentalsandcastles » Fri Mar 30, 2007 4:56 pm

Without seeing a lot more of your code, I couldn't say for absolute certain (i.e. what's in your paint/repaint method?)

I threw your code into eclipse and added some of my applet code, and it appears to work... Take a look at this:

Code: Select all

package jannisery;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JApplet;
import org.jfree.chart.*;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.plot.PlotOrientation;

public class jannisery_problem_applet extends JApplet {
	static final long serialVersionUID=1;
	
	public DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	public ChartPanel chartPanel;
	public JFreeChart chart;
	
	//BELOW IS THE METHOD WE'RE GIVEN TO WORK WITH FROM THE FORUM
	public void DrawChart() {
	      initDataSet();
	      // create the chart...
	      chart = ChartFactory.createBarChart(
	            "Bar Chart", // chart title
	            "Category", // domain axis label
	            "Value", // range axis label
	            dataset, // data
	            PlotOrientation.VERTICAL, // orientation
	            true, // include legend
	            true, // tooltips?
	            false // URLs?
	      );
	      chartPanel = new ChartPanel(chart);
	      
	      //I ADDED THIS FOR TESTING...
	      setContentPane(chartPanel);
	      
	      //I DON'T CARE ABOUT THE PREFERRED SIZE FOR NOW...
	      //chartPanel.setPreferredSize(new Dimension(panel1.getHeight(),panel1.getWidth()));

	      //I DON'T KNOW WHAT PANEL1 IS EXACTLY...
	      //panel1.add(chartPanel);
	      
	      //THIS APPEARS TO BE PART OF THE GUI ... I DON'T CARE ABOUT IT RIGHT NOW...
	      //JOptionPane.showMessageDialog( null, ""+panel1.getComponentCount());
	      
	      //AGAIN ... SINCE WE DON'T HAVE A PANEL1, WE'RE NOT GOING TO USE THIS
	      //panel1.validate();
	      //panel1.repaint();
	}
	
	//EVERYTHING BELOW HERE I ADDED TO MAKE THIS WORK.
	
	public void initDataSet() {
		//INITIALIZE PUBLICLY ACCESSIBLE DATA SET HERE
		return;
	}
	
    public void paint(Graphics g) {
    	//IF WE HAVE A CHART TO DISPLAY...
		if ( chart!=null ) {
			chart.draw( (Graphics2D)g,getBounds());
		}
    }
    
	public void init() {
		DrawChart();
	}
}

Specifically, I added a paint method and an init method that just calls your DrawChart method. Again, without seeing the rest of the code, I couldn't nail down exactly what's wrong with it.[/code]
Have you hugged your computer geek today?

Spyder
Posts: 2
Joined: Fri Mar 30, 2007 6:34 pm

Post by Spyder » Fri Mar 30, 2007 7:43 pm

Try

Code: Select all

public void DrawChart() {
      initDataSet();
      // create the chart...
      JFreeChart chart = ChartFactory.createBarChart(
            "Bar Chart", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
      );
      chartPanel = new ChartPanel(chart);

      panel1.setLayout(new BorderLayout());
      panel1.add(chartPanel, BorderLayout.CENTER);
   }

Locked