List of utility methods to do JTable Column Width Set
int | autoResizeColWidthNoFill(JTable table) auto Resize Col Width No Fill int totalWidth = 0; int headerWidth = 0; table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); int margin = 5; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); ... |
int | calcColumnWidth(int col) calcs the optimal column width of the given column return calcColumnWidth(getJTable(), col);
|
void | chooseGoodColumnWidths(JTable table) Sets the column widths based on the first row. final int PADDING = 5; if (table.getModel().getRowCount() == 0) { return; for (int i = 0; i < table.getModel().getColumnCount(); i++) { TableColumn column = table.getColumnModel().getColumn(i); double headerWidth = table.getTableHeader().getDefaultRenderer() .getTableCellRendererComponent(table, table.getModel().getColumnName(i), false, false, 0, i) ... |
void | fitColumnWidth(JTable table, int colIndex, int padding) Sets the given column of the given table to be exactly as wide as it needs to be to fit its current contents. fitColumnWidth(table, colIndex, -1, padding); |
void | fitColumnWidths(JTable table, int padding) Makes each column of the given table exactly the right size with no constraints on how big the column width would go fitColumnWidths(table, -1, padding); |
void | fitColumnWidths(TableModel model, JTable mainTable) Sets preferred column widths for the table based on header and data content. for (int col = 0; col < model.getColumnCount(); col++) { TableColumn tc = mainTable.getColumnModel().getColumn(col); TableCellRenderer tcr = mainTable.getTableHeader().getDefaultRenderer(); int width = tcr.getTableCellRendererComponent(mainTable, model.getColumnName(col), false, false, 0, col) .getPreferredSize().width + MARGIN; if (model.getRowCount() > 0) tcr = mainTable.getDefaultRenderer(model.getColumnClass(col)); for (int row = 0; row < model.getRowCount(); row++) { ... |
int | getPreferredWidthForColumn(TableColumn col, JTable table) Takes a TableColumn and determines the preferred width for the entire column based on the contents of the row cells. String methodName = MODULE_NAME + "getPreferredWidthForColumn(TableColumn,JTable)"; int retval = 0; int headerWidth = getColumnHeaderWidth(col, table); int cellWidth = getWidestCellInColumn(col, table); retval = headerWidth > cellWidth ? headerWidth : cellWidth; return retval; |
int | getPreferredWidthOfCell(JTable table, int rowIndex, int colIndex) Returns the preferred width of a given cell in a table. int width = 0; TableCellRenderer renderer = table.getCellRenderer(rowIndex, colIndex); Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(rowIndex, colIndex), false, false, rowIndex, colIndex); width = Math.max(width, comp.getPreferredSize().width); return width; |
int | getRenderedWidth(JTable table, int tableColumn, String value) get Rendered Width TableColumn columnToAdjust = table.getColumnModel().getColumn(tableColumn); TableCellRenderer renderer = columnToAdjust.getHeaderRenderer(); if (renderer == null) { JTableHeader header = table.getTableHeader(); renderer = header.getDefaultRenderer(); Component c = renderer.getTableCellRendererComponent(table, value, true, true, -1, tableColumn); int width = c.getPreferredSize().width; ... |
int | getTotalColumnWidth(JTable table) Find out the table width to be used Enumeration<TableColumn> en = table.getColumnModel().getColumns(); int width = 0; while (en.hasMoreElements()) { TableColumn col = en.nextElement(); width += col.getWidth(); return width - 200; |