Import javax.swing.*;
import Javax.swing.table.DefaultTableModel;
import Com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
import java.awt.*;
import Java.util.Vector;
/**
* Test jtable add data, delete data frequently operation, jtable the array of cross-border processing
* In the work if encountered frequent operation JTable data, especially the speed of the situation, often encounter
* Exception in thread "awt-eventqueue-0" Java.lang.ArrayIndexOutOfBoundsException
* Such an array of exceptions, the introduction of swing here a thread, can be a good solution to this problem
* For the same people who encounter such problems for reference.
* @author Chiang's family frenzy
* 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 ("The number of rows deleted cannot exceed the total number of the 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);//This method does not appear to be out of bounds
Addvaluewithoutthread (value);//This method will appear out of bounds, the difference is to join a thread
try {
sleep (10);
} catch (Interruptedexception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
}
}
};
Thread.Start ();
}
/**
* will add records and delete records to go in a thread, do not appear when the page is refreshed, the problem of the array out of bounds
* @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 ();
}
/**
* This side to add records, while deleting records, the array will appear out of bounds
* @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 ();
}
}