Loading and Saving Data and TableModels Begin | Previous | Next
Loading Tab Delimeted Data | Loading CSV Data | Loading Data from a ResultSet | Saving Tab Delimeted Data | Saving CSV Data | Saving HTML Data | Using a TableModel | Virtual ResultSet Model

Loading Tab Delimeted Data
Tab delimited data can be loaded from a file, URL, String or any other stream by using standard Java Reader objects. The data loaded can replace the entire grid or the data can be loaded into a particular block of cells and only replacing the values in those cells.
java.io.Reader data;
...
grid.loadTabDelim( data, true );
Loading CSV Data
Data in a CSV (comma separated value) format can be loaded from a file, URL, String or any other stream by using standard Java Reader objects. The data loaded can replace the entire grid or the data can be loaded into a particular block of cells and only replacing the values in those cells.
java.io.Reader data;
...
grid.loadCSV( data, true );
Loading Data from a ResultSet
Data in a java.sql.ResultSet can be loaded directly into a grid. The data loaded can replace the entire grid or the data can be loaded into a particular block of cells and only replacing the values in those cells. This method will cache all of the values directly in the DsGridModel so the ResultSet will not be needed after the method call has returned. This method will work with any ResultSet object including those from JDBC 1.0.
ResultSet rs;
...
grid.loadResultSet( rs, true );
Saving Tab Delimeted Data
Data in the Grid can be saved to a Tab delimited file, URL, String or any other stream by using standard Java Writer objects. The whole grid and optionally including the column headers can be saved or only block of cell can be saved.
grid.saveAsTabDelim( new File("mydata.txt"), true );
Saving CSV Data
Data in the Grid can be saved to a CSV (comma separated value) file, URL, String or any other stream by using standard Java Writer objects. The whole grid and optionally including the column headers can be saved or only block of cell can be saved.
grid.saveAsCSV( new File("mydata.csv"), true );
Saving HTML Data
Data in the Grid can be saved to a HTML file, URL, String or any other stream by using standard Java Writer objects. The whole grid and optionally including the column headers can be saved or only block of cell can be saved.
grid.saveAsHTML( new File("mydata.html"), true );
Using a TableModel
Any model implementing TableModel can be used to provide data for the Grid. This allows you to retain your investment in any existing TableModels that you may have. All formatting and styles for each cell and sorting are still available.
grid.setModel( new javax.swing.table.AbstractTableModel() {
  public int getRowCount() { return 4; }
  public int getColumnCount() { return 3; }
  public Object getValueAt( int row, int column ) {
    return row + "," + column;
  }
} );
Virtual ResultSet Model
The DsResultSetTableModel will retrieve data from a java.sql.ResultSet on an as needed basis. This means that it will not load the data from the entire query at once and thus reducing memory requirements and increasing performance when working with large amouts of data. All formatting and styles for each cell are still available. It is recommended that for large amounts of data that you set cell properties on a column which will reduce memory requirements substantially.

Note: This requires a JDBC 2.0 driver that supports scrolling cursors in the ResultSet. If that is not available then DsGrid.loadResultSet should be used instead.

java.sql.Connection con;
...
String sql = "SELECT * FROM emp";
DsResultSetTableModel model = new DsResultSetTableModel( con, sql );
grid.setModel( model );
Copyright 2003 Diamond Edge, Inc. All rights reserved.