Swing's jtable use threading a test

Source: Internet
Author: User
Tags rowcount thread

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 ();


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.