我在 jframe 中有一个 jpanel,它正在将数据库中的数据显示到 jtable 中。现在我想添加计时器以在一段时间后刷新这个 jtable,但我没有找到在 jpanel 中添加计时器代码的位置。
这是我的代码..
orgR.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
final JLabel l = new JLabel("Live CDRRecord");
l.setBounds(320, 50, 500, 60);
l.setFont((new Font("verdana", Font.BOLD, 25)));
final Vector columnNames = new Vector();
final Vector data = new Vector();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("");
String sql = "Select calldate,source,destination,extension,trunk,duration,toc,callcost from table";
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
columnNames.add("Calldate");
columnNames.add("Source");
columnNames.add("Destination");
columnNames.add("Extension");
columnNames.add("Trunk");
columnNames.add("Duration");
columnNames.add("TOC");
columnNames.add("CallCost");
while (resultSet.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(resultSet.getObject(i));
}
data.addElement(row);
}
resultSet.close();
statement.close();
} catch (Exception ev) {
System.out.println(ev);
}
final JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer,
int rowIndex, int vColIndex) {
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
if (rowIndex % 2 == 0 && isRowSelected(rowIndex)) {
c.setBackground(Color.LIGHT_GRAY);
} else {
// If not shaded, match the table's background
c.setBackground(getBackground());
}
return c;
}
};
table.setBackground(Color.WHITE);
table.setModel(new DefaultTableModel(data, columnNames));
TableColumn column;
for (int i = 0; i < table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
column.setMaxWidth(800);
}
final JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 120,920,400);
p2.add(l);
p2.add(scrollPane);
p2.setBounds(00, 00, 1400, 650);
p2.add(menubar);
p2.setLayout(null);
Container contentPane = frame.getContentPane();
contentPane.removeAll();
contentPane.add(p2);
contentPane.invalidate();
contentPane.repaint();
}
public void menuDeselected(MenuEvent e) {
// System.out.println("FileTwo.menuDeselected");
orgR.removeAll();
}
public void menuCanceled(MenuEvent e) {
// System.out.println("FileTwow.menuCanceled");
orgR.removeAll();
}
});
任何帮助将不胜感激..在此先感谢..