Java如何实现自动刷新

2024-12-27 09:54:34
推荐回答(3个)
回答1:

while (rs.next()) {

rowData[i][0] = rs.getString("exam_No");
rowData[i][1] = rs.getString("student_ID");
rowData[i][2] = rs.getString("student_Name");
rowData[i][3] = rs.getString("class_NO");
rowData[i][4] = rs.getString("course_Name");
rowData[i][5] = rs.getString("result");
i++;
}
//在此处添加如下代码:
xxxxxxxxxxxx_model.fireTableDataChanged();
//或:
table.setModel(new XxxxxxxxxxTableModel(rowData,.......));

解决方法1:
如果你的表格模型扩展自AbstractTableModel,
表格数据改变后,则调用模型的fireTableDataChanged()方法;

解决方法2:
重新设置表格模型.
例如:
table.setModel(new DefaultTableModel(data,columnNames));

下面是我写的一个上例子.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class JTableTest
{
public static void main(String args[])
{
JTableTestFrame f=new JTableTestFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

class JTableTestFrame extends JFrame
{
private JTable table=null;
private ArrayTableModel atm=null;
private JButton reload=null;
private final static String[] columnNames={"Number","addend","augend","sum"};
private int start=0;
public JTableTestFrame()
{
atm=new ArrayTableModel(columnNames);
table=new JTable(atm);
reload=new JButton("Reload");
reload.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reload();
}
});
JPanel p=new JPanel(new BorderLayout());
p.add(new JLabel("JTable Test"),BorderLayout.NORTH);
p.add(new JScrollPane(table),BorderLayout.CENTER);
p.add(reload,BorderLayout.SOUTH);
this.add(p);
this.setSize(300,300);
this.setVisible(true);

}

public void reload()
{
int rows=10;
int cols=columnNames.length;
Object[][] data=new Object[rows][cols];
for(int i=0;i {
start+=i;
data[i][0]=start;
data[i][1]=start+1;
data[i][2]=start+2;
data[i][3]=start*2+3;
}

atm.setData(data);
atm.fireTableDataChanged();
}
}

class ArrayTableModel extends AbstractTableModel
{
private String[] columnNames=null;
private Object[][] data=null;

public ArrayTableModel(String[] columnNames)
{
this.columnNames=columnNames;
this.data=new Object[1][columnNames.length];
}
public ArrayTableModel(Object[][] data,String[] columnNames)
{
this.data=data;
this.columnNames=columnNames;
}
public Object getValueAt(int r,int c)
{
return this.data[r][c];
}
public int getRowCount()
{
return this.data.length;
}
public int getColumnCount()
{
return this.columnNames.length;
}
public String[] getColumnNames() {
return columnNames;
}
public String getColumnName(int c)
{
return this.columnNames[c].toString();
}
public void setColumnNames(String[] columnNames) {
this.columnNames = columnNames;
}
public Object[][] getData() {
return data;
}
public void setData(Object[][] data) {
this.data = data;
}
}

回答2:

<%
//页面每隔30秒自动刷新一遍
response.setHeader("refresh" , "30" );
%>

回答3:

ajax