Search code examples
javaarraysjpaneljfreechart

Trouble putting my Java FreeChart into the JPanel


I have the following code: However i cant seem to put my output into the JPanel i have created, i have experimented but i can either create the chart and make it appear in a separate chart frame window or it doesnt appear at all, , can someone assist?

package javaapplication1;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;





public class JavaApplication1 extends JFrame implements ActionListener  {

JPanel panel;
Container window = getContentPane(); 
ChartFrame cframe;
private ArrayList<Solar> sols;

public static void main(String[] args) throws IOException {

JavaApplication1 demo = new JavaApplication1();
demo.setPreferredSize(new Dimension(1000,500));
demo.createGUI();



demo.setVisible(true);
}

private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);

window.setLayout(new FlowLayout());
panel = new JPanel();
JButton button = new JButton("Get Forecast");

panel.setBackground(Color.white);
panel.setPreferredSize(new Dimension(1000,400));

//ChartFrame cframe = new ChartFrame("First", chart); 

window.add(panel);
window.add(button);
button.addActionListener(this);
pack();
    }

    @Override
public void actionPerformed(ActionEvent e) {
sols = new ArrayList();
BufferedReader crunchifyBuffer = null;
final String DELIMITER = ",";


    try {
        crunchifyBuffer = new BufferedReader(new FileReader("C:\\Users\\xxxx\\Documents\\Molar Forecasts\\fs-rad.20170421.21.csv"));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
    }
                String line = null;
    try {
        line = crunchifyBuffer.readLine(); // Read first line
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
    }
if (line != null) { try {
    // Check it's not null
    //   line = crunchifyBuffer.readLine(); // Read second line
    while ((line = crunchifyBuffer.readLine()) != null)
    {
        //Get all tokens available in line
        String[] tokens = line.split(DELIMITER);
        Solar sol = new Solar(tokens[0], tokens[1], tokens[2], Double.parseDouble(tokens[3]));

        sols.add(sol);

    }   } catch (IOException ex) {
        Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
    }
processdata();
}}




    public void processdata() {
       System.out.println(sols.size());

 DefaultCategoryDataset dataset = new DefaultCategoryDataset();        

for (int index = 1 ; index <sols.size ();index++){
dataset.addValue(sols.get(index).details1(), "Molar Irradiation", (sols.get(index).details()));

}
JFreeChart chart = ChartFactory.createBarChart( 
"Bar Chart Demo", 
// chart title 
"Category", // domain axis label 
"Value", // range axis label 
dataset, // data 
PlotOrientation.VERTICAL, // orientation 
true, // include 
true, // 
false // URLs? 
);

// create and display a frame... 

add(panel);
pack(); 
//panel.add(cframe,BorderLayout.CENTER);
panel.validate();
//
//
setVisible(true);
    }
        }

Solution

  • In your example, you never add() the chart to any container enclosed by the JFrame, JavaApplication1. ChartPanel is a convenient choice "for displaying a JFreeChart object."

    JFreeChart chart = ChartFactory.createBarChart(…);
    this.add(new ChartPanel(chart));
    this.pack();
    this.setVisible(true);
    

    Note that pack() invokes validate() implicitly, so an explicit call should't be required.