auto-adjust for TableColumn.width / FontMetrics [message #437723] |
Wed, 09 June 2004 12:08  |
Eclipse User |
|
|
|
hi,
I have problems determining the optimal column-width in
my table, since .pack() does not work properly.
1) I want to set table.width for columns that do have
a fixed size.
2) that, I want to size all columns to the minimum-size
required for the largest cell-content.
3) the remaining space of the table is to be shared
among the columns, that doesn't have a fixed size.
Therefor I wrote this code:
<pre>
public void autoAdjustWidth() {
int linesCount = table.getItemCount();
// if there is at least on row
if(linesCount>0){
int columnCount = table.getColumnCount();
ArrayList sizeableCols = new ArrayList();
int totalColWidth = 0;
// Size Columns
for(int j=0; j<columnCount;j++){
TableColumn tableColumn = table.getColumn(j);
String width = (String)tableColumn.getData("width");
if (width == null) {
sizeableCols.add (tableColumn);
problem is here: >>> tableColumn.pack();
totalColWidth += tableColumn.getWidth();
} else {
int remember = tableColumn.getWidth();
tableColumn.setWidth(new Integer(width).intValue());
totalColWidth += tableColumn.getWidth();// - remember;
}
}
// Fill the remaining gap
if (sizeableCols.size() > 0) {
int tableCoreWidth = 40;
// do we have a scrollbar consuming space?
if (linesCount > linesVisibleInTable)
tableCoreWidth = 20;
int restSpace = table.getSize().x - totalColWidth - tableCoreWidth;
int step = restSpace / sizeableCols.size();
while (restSpace > 0) {
for (Iterator it = sizeableCols.iterator();it.hasNext();) {
TableColumn col = (TableColumn) it.next();
col.setWidth(col.getWidth() + step);
restSpace -= step;
if (restSpace == 0)
break;
}
step = 1;
}
}
}
}
</pre>
unfortunatly pack() does not work properly. besides, I am using CellEditors
that have a bigger font. So a solution might be to use FontMetrics and
calculate the size manually. But die class is really - well - unusable.
So my question is: am I missing s.th. concerning the capabilities of
the Table.Class? Is there anopther class helping me determining the
width of a text than FontMetrics? Does noone have such problems?
thanks
tobi
|
|
|
|
Powered by
FUDForum. Page generated in 0.03384 seconds