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
蔣家狂潮 閱讀(1774)
評論(3) 編輯 收藏 所屬分類:
Swing