1 7 8 package org.jdesktop.swing.data; 9 10 import java.sql.ResultSetMetaData ; 11 12 import java.util.ArrayList ; 13 14 import javax.sql.RowSet ; 15 16 import javax.swing.table.AbstractTableModel ; 17 import javax.swing.table.TableModel ; 18 import javax.swing.event.TableModelListener ; 19 import javax.swing.event.TableModelEvent ; 20 21 30 public class RowSetAdapter extends AbstractTableModel { 31 private RowSet rowset; 32 private ResultSetMetaData metaData; 33 34 45 private RowSetAdapter(RowSet rowset) throws java.sql.SQLException { 46 if (rowset == null) { 47 throw new NullPointerException ("rowset cannot be null"); 48 } 49 this.rowset = rowset; 50 this.metaData = rowset.getMetaData(); 51 } 52 53 public Class getColumnClass(int columnIndex) { 54 Class klass = null; 55 try { 56 klass = Class.forName(metaData.getColumnClassName( 57 translateAdapterColumn(columnIndex))); 58 } catch (Exception e) { 59 60 } 61 return klass; 62 } 63 64 public int getRowCount() { 65 return 0; 68 } 69 70 public int getColumnCount() { 71 int columnCount = 0; 72 try { 73 columnCount = metaData.getColumnCount(); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 return columnCount; 78 } 79 80 public Object getValueAt(int rowIndex, int columnIndex) { 81 Object value = null; 82 try { 83 synchronized(rowset) { 84 rowset.absolute(translateAdapterRow(rowIndex)); 85 value = rowset.getObject(translateAdapterColumn(columnIndex)); 86 } 87 } catch (Exception e) { 88 89 } 90 return value; 91 } 92 93 public boolean isCellEditable(int rowIndex, int columnIndex) { 94 boolean editable = false; 96 try { 97 editable = metaData.isWritable(translateAdapterColumn(columnIndex)); 98 } catch (Exception e) { 99 100 } 101 return editable; 102 } 103 104 protected int translateAdapterColumn(int columnIndex) { 105 return columnIndex + 1; 106 } 107 108 protected int translateDataColumn(int dataColumnIndex) { 109 return dataColumnIndex - 1; 110 } 111 112 protected int translateAdapterRow(int rowIndex) { 113 return rowIndex + 1; 114 } 115 116 protected int translateDataRow(int dataRowIndex) { 117 return dataRowIndex - 1; 118 } 119 120 } 121 | Popular Tags |