Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

Prepare TableModel for concurrent access

When refreshing data for a TableModel, always think about multi-threading - never clear the main data and then load it, but instead use a separate storage to load the data, and then switch storages. For example, never use

  MyModel implements TableModel{
   private ArrayList data;
   ...
   private void reload(){
     data.clear();
     jdbcDataSource.readInto(data);// read data from database
     fireModelChanged(); // notify listeners
   }
  }

This will result in hard to find bugs, because getRowCount() and getColumnCount() might be called on incomplete data. So, always use the following technique:

  private void reload(){
    ArrayList a1=new ArrayList();
    jdbcDataSource.readInto(a1); // read data from database
    data=a1;
    fireModelChanged(); // notify listeners 
  }

See also

 

Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  best/prepare_table_model_for_concurrency.txt · Last modified: 2005/03/13 11:54 by 195.137.75.14 (gojko)
 
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources

Sedo - Buy and Sell Domain Names and Websites project info: swingwiki.org Statistics for project swingwiki.org etracker� web controlling instead of log file analysis