隨筆-50  評論-55  文章-8  trackbacks-0

          import javax.swing.*;
          import javax.swing.table.DefaultTableModel;
          import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
          import java.awt.*;
          import java.util.Vector;
          /**
          ?* 測試JTable添加數據,刪除數據頻繁操作,JTable出現數組越界的處理
          ?* 在工作中如果遇到頻繁的操作Jtable的數據,特別是速率很快的情況下,經常會遇到
          ?* Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException
          ?* 這樣的數組越界的異常,這里引入Swing的一個線程,能很好的解決這個問題
          ?* 供同樣遇到這樣問題的人參考。
          ?* @author?蔣家狂潮
          ?* email:simon1006@163.com
          ?*
          ?*/
          public class ThreadTable extends JTable {
          ?private DefaultTableModel model;

          ?static String[] header = new String[] { "id", "name", "sex", "age" };

          ?public ThreadTable() {
          ??model = new DefaultTableModel(header, 0);
          ??this.setModel(model);
          ?}

          ?public void deleteRows(int rowCount) throws Exception {
          ??if (rowCount >= model.getColumnCount()) {
          ???throw new Exception("刪除的行數不能超過model的總行數!");
          ??} else {
          ???for (int i = rowCount - 1; i >= 0; i--) {
          ????model.removeRow(i);
          ???}
          ??}
          ?}

          ?public void testInsertValue() {
          ??final Vector<String> value = new Vector<String>();
          ??value.add("0");
          ??value.add("simon");
          ??value.add("boy");
          ??value.add("21");

          ??Thread thread = new Thread() {
          ???public void run() {
          ????for (int i = 0; i < 100000; i++) {
          ?????//addValueWithThread(value);//這個方法不會出現越界
          ?????addValueWithoutThread(value);//這個方法會出現越界,差別就在于加入一個線程
          ?????try {
          ??????sleep(10);
          ?????} catch (InterruptedException e) {
          ??????// TODO Auto-generated catch block
          ??????e.printStackTrace();
          ?????}
          ????}
          ???}
          ??};
          ??thread.start();
          ?}
          ??? /**
          ???? * 將添加記錄和刪除記錄在一個線程里走,不會出現頁面刷新的時候,數組越界的問題
          ???? * @param value
          ???? */
          ?public void addValueWithThread(final Vector value) {
          ??Thread thread = new Thread() {
          ???public void run() {
          ????Runnable runnable = new Runnable() {
          ?????public void run() {
          ??????model.addRow(value);
          ??????if (model.getRowCount() > 5) {
          ???????try {
          ????????deleteRows(2);
          ???????} catch (Exception e) {
          ????????// TODO Auto-generated catch block
          ????????e.printStackTrace();
          ???????}
          ??????}
          ?????}
          ????};
          ????SwingUtilities.invokeLater(runnable);
          ???}
          ??};
          ??thread.start();
          ?}
          ?/**
          ? * 這樣一邊添加記錄,一邊刪除記錄,會出現數組越界的情況
          ? * @param value
          ? */
          ?public void addValueWithoutThread(final Vector value) {
          ??????model.addRow(value);
          ??????if (model.getRowCount() > 5) {
          ???????try {
          ????????deleteRows(2);
          ???????} catch (Exception e) {
          ????????// TODO Auto-generated catch block
          ????????e.printStackTrace();
          ???????}
          ??????}
          ?????
          ????
          ?}
          ?public static void main(String[] args) {
          ??try {
          ???UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
          ??} catch (UnsupportedLookAndFeelException e) {
          ???// TODO Auto-generated catch block
          ???e.printStackTrace();
          ??}

          ??JFrame f = new JFrame();
          ??f.getContentPane().setLayout(new BorderLayout());

          ??ThreadTable table = new ThreadTable();
          ??JScrollPane scroll = new JScrollPane(table);
          ??f.getContentPane().add(scroll, BorderLayout.CENTER);

          ??f.setSize(800, 600);
          ??f.setLocation(250, 250);
          ??f.setVisible(true);

          ??table.testInsertValue();
          ?}
          }

          posted on 2008-07-30 11:48 蔣家狂潮 閱讀(1777) 評論(3)  編輯  收藏 所屬分類: Swing

          評論:
          # re: Swing之JTable運用線程一個測試[未登錄] 2008-07-30 22:37 | gavin
          這個是什么原因呢??  回復  更多評論
            
          # re: Swing之JTable運用線程一個測試[未登錄] 2008-08-01 16:35 | wolfsquare
          關鍵在SwingUtilities.invokeLater(runnable);上,和線程沒關系  回復  更多評論
            
          # re: Swing之JTable運用線程一個測試 2008-08-04 15:51 | Qil.Wong
          嗯,wolfsquare說的對,SwingUtilities.invokeLater(runnable)將runnable操作放在Swing的EDT線程上,會挨個兒執行,不會同時執行,就不會產生溢出。這個和SwingWorker的publish很類似。  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 靖安县| 商都县| 舞钢市| 哈密市| 荆门市| 桃园市| 和硕县| 教育| 客服| 英超| 大厂| 安国市| 扎兰屯市| 芮城县| 泽库县| 大新县| 阿鲁科尔沁旗| 绍兴县| 洛浦县| 东海县| 塔城市| 启东市| 玛沁县| 城步| 临邑县| 镇康县| 葫芦岛市| 玉林市| 紫云| 定州市| 牡丹江市| 化州市| 兴义市| 霍城县| 宜君县| 安宁市| 新田县| 清远市| 武城县| 正镶白旗| 龙泉市|