Monday, November 5, 2007

Getting Auto-Resizing TableColumns the Easy Way




One of the less prominent novelties in Eclipse 3.3 is the new and shining TableColumnLayout, which re-adjusts the column widths in a table automatically when that table is resized. Previously this was only possible by installing a ControlListener and doing the required work yourself (*ouch*). Read on to learn how you can use it to save yourself some work.

Using the TableColumnLayout

// 1
Composite comp = new Composite(shell, SWT.NONE);
Table table = new Table(comp, SWT.BORDER SWT.V_SCROLL);
table.setHeaderVisible(true);
table.setLinesVisible(true);
// 2
TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
// 3
TableColumnLayout layout = new TableColumnLayout();
comp.setLayout( layout );
// 4
layout.setColumnData( column1, new ColumnWeightData( 30 ) );
layout.setColumnData( column2, new ColumnWeightData( 60 ) );


  1. One peculiar thing about the TableColumnLayout is that you do not apply it to the Table directly. Instead the Table has to be inside a composite that contains only the Table and nothing but the Table. See also Step 3.

  2. We then proceed and create the TableColumns normally.

  3. We create a TableColumnLayout and set it onto the containing Composite.

  4. Using layout.setColumnData(...) we associate ColumnWeightData instances with each column in the Table. Those objects specify how the width is distributed amongst the columns in the table. The most common constructor is new ColumnWeightData( weight ), where weight is an integer that controls the relative width of the column (i.e. relativeWidth = weight / sum( weights of all columns ) ). By the way, you could also use ColumnPixelData to associate a fixed width with a column but since we want auto-resizing columns we'll skip that.

To see how much work we saved compare our snippet with an SWT-Snippet using a Control Listener to do the same job.

Known Limitations

Before you start using the TableColumnLayout you should also be aware of the following limitations:

  • the minimumWidth and (non)resizable settings of the ColumnWeightData are not supported properly
  • the layout stops resizing columns that have been resized manually by the user (this could be considered a feature though)

Snippets

  1. Full TableColumnLayout Example (from above)
  2. TableColumnLayout JFace Snippet
  3. TreeColumnLayout JFace Snippet (this is the equivalent layout but for Trees)

6 comments:

moovida said...

Hi, sorry if I ask in a comment. Is this site maintained any longer? I found your posts really usefull.

Elias Volanakis said...

Hi Moovida,

thanks, I'm glad you found it useful.

I haven't forgotten the blog, but I've been too busy with other things lately.

I intend to start posting again after February.

Regards,
Elias.

moovida said...

That is good news, thanks for your effort!

I definitely want to keep you in my bookmarks :)

moovida said...

I just now notice your great way of formatting and styling the code snippets... wouldn't you please share the way you make this so readable?

Elias Volanakis said...

Hi Moovida,

yes, I put in some extra effort for the snippets. This is how it works:

* First I make sure I've a HTML formatted version of the code, that I can paste into the article.

- For large snippets I use GNU source-highlight (aka java2html). If you've linux, chances are it is included with your distro. Windows users need to compile it, which is rather involved since it needs (cygwin||mingw)&&boost.

- For small snippets I paste to OpenOffice (this preserves formatting). I then save the OpenOffice document as .html. I then copy again from OO and paste into web interface. This results in a html formatted version of the code.

* In blogger I paste the snippet into a table. I then manually (this is a pain) insert   characters to preserve the indenting.

I wish the process was easier. Maybe one day I'll be bothered enough to write a plug-in for copy/pasting java-to-html-code from inside Eclipse.

Kind regards,
Elias.

Anonymous said...

thanks a loooot!!

 

Content: © Copyright 2007 - 2009, Elias Volanakis. All rights reserved. Datenschutzhinweis / Privacy Policy