Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » auto-adjust for TableColumn.width / FontMetrics
auto-adjust for TableColumn.width / FontMetrics [message #437723] Wed, 09 June 2004 16:08 Go to next message
Tobias Weih is currently offline Tobias WeihFriend
Messages: 24
Registered: July 2009
Junior Member
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
Re: auto-adjust for TableColumn.width / FontMetrics [solved] [message #438298 is a reply to message #437723] Mon, 21 June 2004 09:33 Go to previous message
Tobias Weih is currently offline Tobias WeihFriend
Messages: 24
Registered: July 2009
Junior Member
ok, this works (instead of pack()):

public int stringWidth(String str)
{
GC g = new GC(table.table);
Font of = g.getFont();
g.setFont(table.EDITOR_FONT);
Point extent = g.stringExtent(str);
g.setFont(of);
return extent.x;
}
Previous Topic:how eclipse plugin could load the classes from the system's CLASSPATH ?
Next Topic:classpath
Goto Forum:
  


Current Time: Thu Sep 26 20:56:16 GMT 2024

Powered by FUDForum. Page generated in 0.03090 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top