This is a program to display a pie chart. I also have a button. But the program I have written, creates two separate frames and one frame is for the pie chart and the other is for the button. How do I put both button and pie chart on the same frame?? Can you please rectify my program.
import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
public class Pie{
public static void getFile(File f)
{
System.out.println("File is: "+f.getName());
}
public static void main(String[] args)
{
JPanel panel=new JPanel();
panel.setLayout(null);
JButton b=new JButton("Open File");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser=new JFileChooser();
int ret = chooser.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
getFile(file);
}
}
}
);
b.setBounds(50,40,100,35);
panel.add(b);
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("A", new Integer(10));
pieDataset.setValue("B", new Integer(20));
pieDataset.setValue("C", new Integer(30));
pieDataset.setValue("D", new Integer(10));
pieDataset.setValue("E", new Integer(20));
pieDataset.setValue("F", new Integer(10));
JFreeChart chart = ChartFactory.createPieChart("Pie Chart using JFreeChart", pieDataset, true,true,true);
ChartFrame frame1=new ChartFrame("Pie Chart",chart);
frame1.add(panel);
frame1.setVisible(true);
frame1.setSize(600,600);
}
}