import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
import java.awt.*;
import java.util.Vector;
/**
?* 測(cè)試JTable添加數(shù)據(jù),刪除數(shù)據(jù)頻繁操作,JTable出現(xiàn)數(shù)組越界的處理
?* 在工作中如果遇到頻繁的操作Jtable的數(shù)據(jù),特別是速率很快的情況下,經(jīng)常會(huì)遇到
?* Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException
?* 這樣的數(shù)組越界的異常,這里引入Swing的一個(gè)線程,能很好的解決這個(gè)問(wèn)題
?* 供同樣遇到這樣問(wèn)題的人參考。
?* @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ù)不能超過(guò)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);//這個(gè)方法不會(huì)出現(xiàn)越界
?????addValueWithoutThread(value);//這個(gè)方法會(huì)出現(xiàn)越界,差別就在于加入一個(gè)線程
?????try {
??????sleep(10);
?????} catch (InterruptedException e) {
??????// TODO Auto-generated catch block
??????e.printStackTrace();
?????}
????}
???}
??};
??thread.start();
?}
??? /**
???? * 將添加記錄和刪除記錄在一個(gè)線程里走,不會(huì)出現(xiàn)頁(yè)面刷新的時(shí)候,數(shù)組越界的問(wèn)題
???? * @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();
?}
?/**
? * 這樣一邊添加記錄,一邊刪除記錄,會(huì)出現(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();
?}
}