我遇到了JFreeChart与Java的一些问题。 我正在从远程数据库中检索数据,以便从我的应用程序中显示在Panel中。
问题:当我获取数据时,它不会直接显示在面板上。我必须最小化并最大化表单才能加载数据。
我需要每分钟刷新这些数据。我在想Timer
,但我真的不知道如何整合它。
以下是代码的一部分:
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 ();}
}
}}
这是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;
}
}
我该如何解决这个问题?有人做过类似的吗? 感谢
答案 0 :(得分:2)
我可能忽略了JFreeChart问题,但在event dispatch thread上发生了Swing 中的绘图。相反,您对displayPie()
的来电来自Runnable
中的匿名GetRequest
。常见的补救措施包括: