我有一个显示我的数据的JTable。这两个JTable包含两个Column(col1表示Time,col2表示CO2传感器的值)。 单击“开始”按钮,数据将添加到表中,并带有我可以通过控制台输入的句点
在每次更改Koordianten x = time,y = CO2值之后,必须显示折线图动态。
如何动态显示此线图。 这是我的代码:
public PanelGraphic() throws NotConnectedException {
initComponents();
}
private static JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"KohlendioxdeTest", "Time ", "CO2 (ppm)", dataset,
PlotOrientation.VERTICAL, false, false, false);
return chart;
}
private void display(){
JFreeChart chart = createChart(dataset);
ChartPanel panel = new ChartPanel(chart);
final XYPlot plot = (XYPlot) chart.getPlot();
panel.setPreferredSize(new java.awt.Dimension(515, 265));
PlGraph.setLayout(new java.awt.BorderLayout());
PlGraph.add(panel, BorderLayout.CENTER);
PlGraph.validate();
}
private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {
final DefaultTableModel model = (DefaultTableModel) tbCO2Value.getModel();
try {
if (!txtSetPeriode.getText().trim().isEmpty()) {
double peri = Long.parseLong(txtSetPeriode.getText());
co2.setCO2CallbackPeriod((long) peri * 1000);
co2.addCO2PPMListener(new BrickletCO2.PPmListener() {
@Override
public void PPmconverter(int kohlendioxide) {
model.addRow(new Object[]{DisplayTime.getTime(), String.valueOf(kohlendioxide)});
}
});
} else {
lbComment.setText("Bitte geben sie die Periode ein");
}
} catch (NotConnectedException ex) {
Logger.getLogger(PanelGraphic.class.getName()).log(Level.SEVERE, null, ex);
} catch (TimeoutException ex) {
Logger.getLogger(PanelGraphic.class.getName()).log(Level.SEVERE, null, ex);
}
tbCO2Value.getModel().addTableModelListener(new MyTableModelListener(tbCO2Value));
dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("CO2 (ppm)");
int nRow = tbCO2Value.getRowCount();
int nCol = tbCO2Value.getColumnCount();
Object[][] tableData = new Object[nRow][nCol];
for (int i = 0; i < nRow; i++) {
tableData[i][0] = tbCO2Value.getValueAt(i, 0);
tableData[i][1] = tbCO2Value.getValueAt(i, 1);
// for (int i = 0; i < 10; i++) {
series.add((double) tableData[i][0],(double) tableData[i][1] );
}
dataset.addSeries(series);
display();
}
class MyTableModelListener implements TableModelListener {
JTable table;
private MyTableModelListener(JTable table) {
this.table = table;
}
@Override
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int index = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
int nRow = tbCO2Value.getRowCount();
int nCol = tbCO2Value.getColumnCount();
Object[][] tableData = new Object[nRow][nCol];
for (int i = 0; i < nRow; i++) {
tableData[i][0] = tbCO2Value.getValueAt(i, 0);
tableData[i][1] = tbCO2Value.getValueAt(i, 1);
}
break;
}
}
}
}
答案 0 :(得分:2)
就在JTable
收听TableModel
时,XYPlot
会收听自己的XYDataset
。在每种情况下,只需更新相关模型,相应的视图将自行更新。使用javax.swing.Timer
以所选期间规定的费率轮询您的数据源。显示了一个相关示例here。概括地说,您的计时器ActionListener
可能如下所示:
@Override
public void actionPerformed(ActionEvent e) {
// poll the data source
model.add(Row(…);
dataset.add(…);
}
如果您预计访问数据源的延迟,请考虑使用SwingWorker
,图示为here和here。