1 65 66 67 package org.hsqldb.util; 68 69 import java.util.Vector ; 70 import java.awt.Component ; 71 72 import javax.swing.JTable ; 73 import javax.swing.event.TableModelEvent ; 74 import javax.swing.table.AbstractTableModel ; 75 import javax.swing.table.TableCellRenderer ; 76 import javax.swing.table.TableColumn ; 77 import javax.swing.table.TableModel ; 78 79 82 90 class GridSwing extends AbstractTableModel { 91 92 JTable jtable = null; 93 Object [] headers; 94 Vector rows; 95 96 99 public GridSwing() { 100 101 super(); 102 103 headers = new Object [0]; rows = new Vector (); } 106 107 110 public String getColumnName(int i) { 111 return headers[i].toString(); 112 } 113 114 public Class getColumnClass(int i) { 115 116 if (rows.size() > 0) { 117 Object o = getValueAt(0, i); 118 119 if (o != null) { 120 return o.getClass(); 121 } 122 } 123 124 return super.getColumnClass(i); 125 } 126 127 130 public int getColumnCount() { 131 return headers.length; 132 } 133 134 137 public int getRowCount() { 138 return rows.size(); 139 } 140 141 144 public Object [] getHead() { 145 return headers; 146 } 147 148 155 public Vector getData() { 156 return rows; 157 } 158 159 162 public Object getValueAt(int row, int col) { 163 164 if (row >= rows.size()) { 165 return null; 166 } 167 168 Object [] colArray = (Object []) rows.elementAt(row); 169 170 if (col >= colArray.length) { 171 return null; 172 } 173 174 return colArray[col]; 175 } 176 177 180 public void setHead(Object [] h) { 181 182 headers = new Object [h.length]; 183 184 for (int i = 0; i < h.length; i++) { 186 headers[i] = h[i]; 187 } 188 } 189 190 193 public void addRow(Object [] r) { 194 195 Object [] row = new Object [r.length]; 196 197 for (int i = 0; i < r.length; i++) { 199 row[i] = r[i]; 200 201 if (row[i] == null) { 202 203 } 205 } 206 207 rows.addElement(row); 208 } 209 210 214 public void clear() { 215 rows.removeAllElements(); 216 } 217 218 public void setJTable(JTable table) { 219 jtable = table; 220 } 221 222 public void fireTableChanged(TableModelEvent e) { 223 super.fireTableChanged(e); 224 autoSizeTableColumns(jtable); 225 } 226 227 public static void autoSizeTableColumns(JTable table) { 228 229 TableModel model = table.getModel(); 230 TableColumn column = null; 231 Component comp = null; 232 int headerWidth = 0; 233 int maxCellWidth = Integer.MIN_VALUE; 234 int cellWidth = 0; 235 TableCellRenderer headerRenderer = 236 table.getTableHeader().getDefaultRenderer(); 237 238 for (int i = 0; i < table.getColumnCount(); i++) { 239 column = table.getColumnModel().getColumn(i); 240 comp = headerRenderer.getTableCellRendererComponent(table, 241 column.getHeaderValue(), false, false, 0, 0); 242 headerWidth = comp.getPreferredSize().width + 10; 243 maxCellWidth = Integer.MIN_VALUE; 244 245 for (int j = 0; j < Math.min(model.getRowCount(), 30); j++) { 246 TableCellRenderer r = table.getCellRenderer(j, i); 247 248 comp = r.getTableCellRendererComponent(table, 249 model.getValueAt(j, i), 250 false, false, j, i); 251 cellWidth = comp.getPreferredSize().width; 252 253 if (cellWidth >= maxCellWidth) { 254 maxCellWidth = cellWidth; 255 } 256 } 257 258 column.setPreferredWidth(Math.max(headerWidth, maxCellWidth) 259 + 10); 260 } 261 } 262 } 263 | Popular Tags |