隨筆-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添加數(shù)據(jù),刪除數(shù)據(jù)頻繁操作,JTable出現(xiàn)數(shù)組越界的處理
          ?* 在工作中如果遇到頻繁的操作Jtable的數(shù)據(jù),特別是速率很快的情況下,經(jīng)常會遇到
          ?* Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException
          ?* 這樣的數(shù)組越界的異常,這里引入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("刪除的行數(shù)不能超過model的總行數(shù)!");
          ??} 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);//這個方法不會出現(xiàn)越界
          ?????addValueWithoutThread(value);//這個方法會出現(xiàn)越界,差別就在于加入一個線程
          ?????try {
          ??????sleep(10);
          ?????} catch (InterruptedException e) {
          ??????// TODO Auto-generated catch block
          ??????e.printStackTrace();
          ?????}
          ????}
          ???}
          ??};
          ??thread.start();
          ?}
          ??? /**
          ???? * 將添加記錄和刪除記錄在一個線程里走,不會出現(xiàn)頁面刷新的時候,數(shù)組越界的問題
          ???? * @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();
          ?}
          ?/**
          ? * 這樣一邊添加記錄,一邊刪除記錄,會出現(xiàn)數(shù)組越界的情況
          ? * @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 蔣家狂潮 閱讀(1782) 評論(3)  編輯  收藏 所屬分類: Swing

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 城口县| 玛沁县| 福建省| 库尔勒市| 山丹县| 东兰县| 九寨沟县| 延庆县| 安阳市| 贵定县| 香港| 奉新县| 康定县| 湖南省| 万州区| 获嘉县| 安化县| 葫芦岛市| 镇安县| 章丘市| 时尚| 中方县| 霍林郭勒市| 宁城县| 丹巴县| 通化市| 周至县| 邳州市| 会东县| 贡山| 霞浦县| 天峨县| 谢通门县| 东乌珠穆沁旗| 会昌县| 武鸣县| 大渡口区| 西峡县| 合水县| 清水县| 额济纳旗|