免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2451 | 回复: 0
打印 上一主题 下一主题

使用Swing timer实现Jtable数据定时更新 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-17 23:47 |只看该作者 |倒序浏览

程序分三部分,主程序类SuperTable.java,框架类SuperTableFrame.java,数据模板类SuperTableModel.java。
主程序类SuperTable.java,如下:
package supertable.swing;
import javax.swing.*;
public class SuperTable {
public static void main(String[] args){
  JFrame frame = new SuperTableFrame();
  frame.show();
}
}
框架类SuperTableFrame.java,如下:
package supertable.swing;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.Timer;
/*
* 创建日期 2005-3-21

*/
/**
* @author Administrator
*
*/
public class SuperTableFrame extends JFrame {
private JTable table = null;
private Timer timer = null;
public SuperTableFrame(){
  timer = new Timer(300,updateTableAction);
  setTitle("SuperTable");
  setSize(300,200);
  addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  });
  TableModel model = new SuperTableModel(30,5,10);
  table = new JTable(model);
  
  JPanel p = new JPanel();
  addButton(p,"Update",new ActionListener(){
   public void actionPerformed(ActionEvent evt){
    table.validate();
    table.updateUI();
   }
  });
  addButton(p,"Start",new ActionListener(){
   public void actionPerformed(ActionEvent evt){
    timer.start();
   }
  });
  addButton(p,"Stop",new ActionListener(){
   public void actionPerformed(ActionEvent evt){
    timer.stop();
   }
  });
  Container contentPane = getContentPane();
  contentPane.add(new JScrollPane(table),"Center");
  contentPane.add(p,"South");
}
public void addButton(Container c,String title,ActionListener a){
  JButton b = new JButton(title);
  c.add(b);
  b.addActionListener(a);
}
private Action updateTableAction = new AbstractAction(){
  public void actionPerformed(ActionEvent e){
   table.validate();
   table.updateUI();
  }
};
}
数据模板类SuperTableModel.java,如下:
package supertable.swing;
import java.text.NumberFormat;
import javax.swing.table.AbstractTableModel;
public class SuperTableModel extends AbstractTableModel {
private int years;
private int minRate;
private int maxRate;
private static final double INITAL_BALANCE = 100000.0;
public SuperTableModel(int y,int r1,int r2){
  years = y;
  minRate =r1;
  maxRate = r2;
}
public int getRowCount(){
  return years;
}
public int getColumnCount(){
  return maxRate -minRate +1;
}
public Object getValueAt(int r,int c){
  //double rate = (c + minRate)/100.0;
  //int nperiods = r;
  //double futureBalance = INITAL_BALANCE * Math.pow(1+rate,nperiods);
  
  double rate = Math.random();
  return NumberFormat.getCurrencyInstance().format(rate);
}
public String getColumnName(int c){
  double rate = (c+minRate)/100.0;
  return NumberFormat.getPercentInstance().format(rate);
}
}
运行图:



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15511/showart_101268.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP