public static void maxTableColumnsWidth(Table table) { table.setRedraw(false); try { int columnCount = table.getColumnCount(); if (columnCount > 0) { int totalWidth = 0; final TableColumn[] columns = table.getColumns(); for (TableColumn tc : columns) { tc.pack(); totalWidth += tc.getWidth(); } final Rectangle clientArea = table.getClientArea(); if (totalWidth < clientArea.width) { int extraSpace = clientArea.width - totalWidth; extraSpace /= columnCount; for (TableColumn tc : columns) { tc.setWidth(tc.getWidth() + extraSpace); } } } } finally { table.setRedraw(true); } }
public static void packColumns(Table table, boolean fit) { table.setRedraw(false); try { int totalWidth = 0; final TableColumn[] columns = table.getColumns(); for (TableColumn column : columns) { column.pack(); totalWidth += column.getWidth(); } final Rectangle clientArea = table.getClientArea(); if (clientArea.width > 0 && totalWidth > clientArea.width) { for (TableColumn column : columns) { int colWidth = column.getWidth(); if (colWidth > totalWidth / 3) { // If some columns is too big (more than 33% of total width) // Then shrink it to 30% column.setWidth(totalWidth / 3); totalWidth -= colWidth; totalWidth += column.getWidth(); } } int extraSpace = totalWidth - clientArea.width; for (TableColumn tc : columns) { double ratio = (double) tc.getWidth() / totalWidth; int newWidth = (int) (tc.getWidth() - extraSpace * ratio); tc.setWidth(newWidth); } } if (fit && totalWidth < clientArea.width) { int sbWidth = 0; if (table.getVerticalBar() != null) { sbWidth = table.getVerticalBar().getSize().x; } if (columns.length > 0) { float extraSpace = (clientArea.width - totalWidth - sbWidth) / columns.length; for (TableColumn tc : columns) { tc.setWidth((int) (tc.getWidth() + extraSpace)); } } } } finally { table.setRedraw(true); } }