Setting table row height in SWT

So it appears you can actually set the row height of a SWT table despite me previously saying you couldn’t. The caveat is that it cannot be set on a per row basis. Row height is set using a Listener for SWT.MeasureItem and then setting the height property of the supplied event object as shown below.

// resize the row height using a MeasureItem listener
table.addListener(SWT.MeasureItem, new Listener() {
   public void handleEvent(Event event) {
      // height cannot be per row so simply set
      event.height = 67;
   }
});

If need be you could compute the actual height using the GC.stringExtent(String).y and simply set to the biggest value you encounter.

Information from Custom Drawing Table and Tree Items at Eclipse.org.

14 thoughts on “Setting table row height in SWT”

  1. Yeah, the wrapping works fine in 1.0.1. Some larger entries still get truncated though.

    I was just thinking that if you are going to size the table rows to accomodate the largest entry then one large entry could ‘mess up’ the table for all the normal sized entries.

    I was suggesting that if you picked a ‘reasonable’ height for the rows and added a tooltip then large posts could be read by just moving the mouse over the entry, without having to resize anything.

    Like

  2. i want to do the same with Grid nebula.
    i want it to support newline character so i can insert multiple rows in each cell.

    Like

  3. hi thank you for your code buddy. i really tried hard to find out specifying the height of row. the code worked .thanks again

    Like

  4. Will this still work, if we maually expands or shrinks the column width of the table ?

    I mean if we manually shrink the column width, the row height will grow and if we expand the column width, the row height will fall.

    Like

  5.  Well when I did it the row could only grow in size so you could make it higher as the column was made smaller but it wouldn’t become smaller when the column was made wider. Only way I’ve found to do that is by writing my own table component which I’ve done since then.

    Like

Comments are closed.