Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 931815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:33:53+00:00 2026-05-15T20:33:53+00:00

In a tree with multiple columns, how do I resize columns to contents upon

  • 0

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?!)

X-posted to Eclipse Community Forum

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:33:54+00:00Added an answer on May 15, 2026 at 8:33 pm

    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:

    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();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.