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?!)
A snippet showing your issue would have made this easier.
All you have to do to fix this, is put your listener’s code into an asyncExec block, to postpone its execution. (I have a deja-vu, since I answered another question with the same tip very recently…)
BTW: You don’t have to call TreeItem.setExpanded(.). Also, don’t use Boolean when you have a primitive type. It works, but the VM does auto boxing all the time, which isn’t good for performance. That doesn’t matter here, but it’s a bad habbit. And then: Underscore suffixes for variables?
Now the working snippet, taken and adapted from here: