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
- 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.
- We then proceed and create the TableColumns normally.
- We create a TableColumnLayout and set it onto the containing Composite.
- 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.
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)