TableColumn col = colModel.getColumn(column); int width;
TableColumn col = colModel.getColumn(column); int width;
/** * Returns the GenericTableColumn associated with the given column index. */ @SuppressWarnings("unchecked") // The column model is private, and is known to contain only GenericTableColumns. public GenericTableColumn<T,?> getGenericColumn(int col) { return (GenericTableColumn<T,?>) _columnModel.getColumn(col); }
public TableColumn getColumn(int columnIndex, boolean includeHidden) { if (includeHidden) { return initialColumns.get(columnIndex); } return super.getColumn(columnIndex); }
jTable1.getColumnModel().getColumn(4).setPreferredWidth(100); Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4 at java.util.Vector.elementAt(Vector.java:427) at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
/** * Maps between the visible and model indexes. * @param visibleIndex the visible index * @return the model index */ public int getOriginalIndex(int visibleIndex) { TableColumn column = super.getColumn(visibleIndex); int orgIndex = getOriginalIndex(column); if (orgIndex == -1) { // indicates that somehow the underlying vector has been modified throw new IllegalArgumentException("no such original column"); } return orgIndex; }
TableColumn col = colModel.getColumn(vColIndex); int width = 0;
TableColumn col = colModel.getColumn(vColIndex); int width = 0;
TableColumn col = colModel.getColumn(vColIndex); int width = 0;
private void setParamsTableColumnSizes() { for (int i = 0; i < paramsTable.getColumnCount(); i++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) paramsTable.getColumnModel(); TableColumn col = colModel.getColumn(i); int width = 0; TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = paramsTable.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(paramsTable, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; col.setPreferredWidth(width + 2); } paramsTable.getColumnModel().getColumn(6).setPreferredWidth(999); // value }
TableColumn col = colModel.getColumn(vColIndex);
public static int packColumn(JTable table, int vColIndex, int margin, boolean fixed) { //TableModel model = table.getModel(); DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); if (vColIndex < table.getColumnCount()) { TableColumn col = colModel.getColumn(vColIndex); if (col != null) { if (fixed) { col.setResizable(false); int width = getPreferredColumnWidth(table, vColIndex, margin); if (width != col.getPreferredWidth()) { //System.out.println("Setting first column width to: " + width); col.setMinWidth(width); col.setMaxWidth(width); col.setPreferredWidth(width); } return width; } else { int width = getPreferredColumnWidth(table, vColIndex, margin); col.setPreferredWidth(width); return width; } } } return 0; } // public static int packColumn(JTable table, int vColIndex, int margin, boolean max) {
/** * Resize the column widths according to the displayed data. */ public void autoResizeColumnWidths() { for (int colNdx = 0; colNdx < getColumnCount(); colNdx++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) getColumnModel(); TableColumn col = colModel.getColumn(colNdx); TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(this, col.getHeaderValue(), false, false, 0, 0); int width = comp.getPreferredSize().width; for (int rowNdx = 0; rowNdx < this.getRowCount(); rowNdx++) { renderer = this.getCellRenderer(rowNdx, colNdx); comp = renderer.getTableCellRendererComponent(this, getValueAt(rowNdx, colNdx), false, false, rowNdx, colNdx); width = Math.max(width, comp.getPreferredSize().width); } col.setPreferredWidth(width + 10); // with some margin } }