1

I am facing some problems with JFreeChart with Java. I am retriving data from remote database to make them display in a Panel from my application.

  1. The problem: when I get the data, it does not display directly on the panel. I have to minimize and maximize the form to make the data being loaded.

  2. I need this data to be refreshing every minute. I was thinking about a Timer but I don't really know how to integrate it.

Here is part of the code:

import java.io.BufferedReader;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JPanel;

public class GetRequest implements Runnable{

    public BufferedReader _input = null;
    public Socket clientSocket = null;
    public DrawChart draw;
    public List<Double> values_1, values_2, values_3, values_4;
    public JPanel _panel;

    public GetRequest(Socket socket, JPanel panel){
        clientSocket = socket;
        draw = new DrawChart();
        _panel = panel;
        values_1 = new ArrayList<Double>();
        values_2 = new ArrayList<Double>();
        values_3 = new ArrayList<Double>();
    }

    @Override
    public void run(){
        while(true){

            try{
                _input = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream ()));
                String line = _input.readLine ();

                String[] clineComat = line.split (",");
                for(int i = 0 ;i<clineComat.length;i++){

                    String[] clineSemiComa_ = clineComat[i].split (";");
                    System.out.println(">"+clineComat[i]);
                    values_1.add (Double.parseDouble (clineSemiComa_[0])+1);
                    values_2.add (Double.parseDouble (clineSemiComa_[1])+1);
                    values_3.add (Double.parseDouble (clineSemiComa_[2]));
                }

                EventQueue.invokeLater (new Runnable(){
                public void run(){
                    draw.displayPie (_panel, values_1, values_2, values_3);
                }
            });
            }
            catch(IOException e){e.printStackTrace ();}
        }
    }}

and this is the code of the DrawChart.java

import  org.jfree.chart.ChartFactory;
import  org.jfree.chart.ChartPanel;
import  org.jfree.chart.JFreeChart;
import  org.jfree.data.general.DefaultPieDataset;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import org.jfree.chart.plot.PlotOrientation;
import java.awt.Dimension;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.category.CategoryDataset;
import java.util.List;
import java.lang.Float;
import java.util.Timer;
import java.util.TimerTask;

public class DrawChart {

    public void displayPie(JPanel jpanel, List<Double> values_1, List<Double> values_2, List<Double> values_3) {

        CategoryDataset data = createDataset(values_1, values_2, values_3);
        //create a chart...
        JFreeChart chart=ChartFactory.createBarChart ("", null, null, data, PlotOrientation.VERTICAL, true, true, false);
        chart = ChartFactory.createLineChart ("", null, null, data, PlotOrientation.VERTICAL, true, true, false);

        //create and display a frame...
        ChartPanel panel=new ChartPanel(chart);
        panel.setPreferredSize (new Dimension(700,300));
        jpanel.setLayout(new java.awt.BorderLayout());
        jpanel.add (panel, BorderLayout.CENTER);
        jpanel.setVisible(true);
        }

    public static CategoryDataset createDataset(List<Double> a, List<Double> b, List<Double> c) {

        String series1 = "Series1";
        String series2 = "Series2";
        String series3 = "Series3";

        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        if(a.size () != 0 && b.size () != 0 && c.size () != 0 ) {

            for(int i=0;i<a.size ();i++) {
                dataset.addValue(a.get (i), series1, i+1+"");
            }

            for(int i=0;i<b.size ();i++) {
                dataset.addValue(b.get (i), series2, i+1+"");
            }

            for(int i=0;i<c.size ();i++) {
                dataset.addValue(c.get (i), series3, i+1+"");
            }

        }

        return dataset;
    }
}

How can I solve this ? Does anyone has done a similar ? Thanks

1 Answer 1

2

I may be overlooking a JFreeChart problem, but drawing in Swing must occur on the event dispatch thread. In contrast, your call to displayPie() occurs from an anonymous Runnable in GetRequest. Common remedies include these:

  • EventQueue.invokeLater, illustrated here.

  • SwingWorker, illustrated here.

  • javax.swing.Timer for periodic updates, illustrated here and here.

Sign up to request clarification or add additional context in comments.

2 Comments

hi @trashgod i edited the code and add as you told but nothing happened. still getting the same problem. When the JFreeChart uis loading it is not displayed,, i have to minimize and maximize before I get it viewed in the JPanel
Without your an sscce I'm not sure. You could try revalidate(); repaint() after jpanel.add(). Updating the model would be easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.