In a tree with multiple columns, how do I resize columns to contents upon expand/collapse and data updates? The solution to the similar question for tables doesn't work. Same as
tree.addListener(SWT.Collapse, new Listener(){
@Override
public void handleEvent(Event e) {
expandAndResize(false, (TreeItem) e.item);
}
});
tree.addListener(SWT.Expand, new Listener() {
@Override
public void handleEvent(Event event) {
expandAndResize(false, (TreeItem) event.item);
}
});
private static void expandAndResize(Boolean expand_, TreeItem item_)
{
System.out.println( (expand_?"Expanding":"Collapsing") + "item={" + item_ + "}");
item_.setExpanded(expand_);
System.out.println(" Resizing columns");
resizeTree(item_.getParent());
}
private static void resizeTree(Tree tree_)
{
for (TreeColumn tc: tree_.getColumns())
resizeTreeColumn(tc);
}
private static void resizeTreeColumn(TreeColumn treeColumn_)
{
treeColumn_.pack();
}
This works for data updates (by calling the resizeTree), but for expands/collapses it is one step behind!
More specifically, if I expand an item in the first column, and the underlying item is longer than the column width, the resize to that width will be done upon a next collapse or expand of some other item (or if I directly call resizeTree()).
Just in case it is important: the data is given by TreeContentProvider and ITableLabelProvider, and my guess is that ITableLabelProvider might cause the problem (the width of the column is adjusted before the label gets generated?! )