项目展开时自动调整列大小

时间:2010-07-07 14:15:34

标签: java user-interface tree swt

在具有多列的树中,如何在展开/折叠和数据更新时将列重新调整为内容?

类似question for tables的解决方案不起作用。

同样:

  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();  
 }

这适用于数据更新(通过调用resizeTree),但对于扩展/折叠,它落后一步!

更具体地说,如果我展开第一列中的项目,并且基础项目的长度大于列宽度,则调整该宽度的大小将在下一次折叠或扩展其他项目时完成(或者如果我直接致电resizeTree)。

以防万一重要:数据由TreeContentProviderITableLabelProvider提供,我的猜测是ITableLabelProvider可能导致问题(列的宽度在之前调整)标签生成了吗?!)

X-posted to Eclipse Community Forum

1 个答案:

答案 0 :(得分:5)

显示您的问题的snippet会让这更容易。

要解决此问题,您需要将侦听器的代码放入asyncExec块中,以推迟执行。 (我有一个似曾相识,因为我最近用同样的提示回答了另一个问题......)

BTW:您不必调用TreeItem.setExpanded(。)。此外,如果您具有基本类型,请不要使用布尔值。它可以工作,但VM一直在自动装箱,这对性能不利。这没关系,但这是一个糟糕的习惯。然后:变量的下划线后缀?

现在,工作片段由here

采取并改编
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class Snippet {

   public static void main( String[] args ) {
      final Display display = new Display();
      Shell shell = new Shell(display);
      shell.setLayout(new FillLayout());
      final Tree tree = new Tree(shell, SWT.BORDER);
      tree.setHeaderVisible(true);
      TreeColumn col1 = new TreeColumn(tree, 0);
      col1.setText("col1");
      TreeColumn col2 = new TreeColumn(tree, 0);
      col2.setText("col2");

      for ( int i = 0; i < 4; i++ ) {
         TreeItem iItem = new TreeItem(tree, 0);
         iItem.setText(new String[] { "TreeItem (0) -" + i, "x" });
         for ( int j = 0; j < 4; j++ ) {
            TreeItem jItem = new TreeItem(iItem, 0);
            jItem.setText(new String[] { "TreeItem (1) -" + i, "x" });
            for ( int k = 0; k < 4; k++ ) {
               TreeItem kItem = new TreeItem(jItem, 0);
               kItem.setText(new String[] { "TreeItem (2) -" + i, "x" });
               for ( int l = 0; l < 4; l++ ) {
                  TreeItem lItem = new TreeItem(kItem, 0);
                  lItem.setText(new String[] { "TreeItem (3) -" + i, "x" });
               }
            }
         }
      }

      col1.pack();
      col2.pack();

      Listener listener = new Listener() {

         @Override
         public void handleEvent( Event e ) {
            final TreeItem treeItem = (TreeItem)e.item;
            display.asyncExec(new Runnable() {

               @Override
               public void run() {
                  for ( TreeColumn tc : treeItem.getParent().getColumns() )
                     tc.pack();
               }
            });
         }
      };

      tree.addListener(SWT.Collapse, listener);
      tree.addListener(SWT.Expand, listener);

      shell.setSize(200, 200);
      shell.open();
      while ( !shell.isDisposed() ) {
         if ( !display.readAndDispatch() ) display.sleep();
      }
      display.dispose();
   }
}