Change row height in SWT Table/Tree

Go To


I have a Tree, which is populated with TreeItems that may have an image (a row with a thumbnail). I resize the row height with MeasureItem listener, and everything is good.

But, now I want to make this change dynamic. I check if a row should have an image, and if any row in the currently listed rows has an image that should be displayed, I set the row height to 180px, and all the rows have that height. If there is no row with an image, then the row height should be 25px.

The issue is, if at any point the row height is set to 180px, I can't change it back to 25px. My code looks like this (simplified):

//this check is run on each page of paginated results fetched from DB
for(Result r : results){

    // resize the row height using a MeasureItem listener
    tree.addListener(SWT.MeasureItem, new Listener() {
        public void handleEvent(Event event) {
            event.height = rowHeight;

    //added 'event.height=rowHeight' here just to check if it will draw as I want
    tree.addListener(SWT.EraseItem, new Listener() {
        public void handleEvent(Event event) {
                event.detail &= ~SWT.FOREGROUND;

The rowHeight variable is set as it should, and also the 'event.height' in the MeasureListener is set to the same value. But still doesn't want to reduce the height. On every page of results I'm clearing the items with tree.removeAll(). Maybe I should dispose and recreate the table/tree (which I don't want to do)?

So, any idea how to do it?

P.S. Not to be confused, I don't need various heights on the rows, I know that it's not possible under Windows/Mac. I just need a way to reset the height.

2012-04-04 01:09
by ekstrakt


The fact that you can't decrease the height of the rows is a known bug.

2012-04-05 14:04
by p12t
Thank you for pointing the bug. I ended redrawing the whole table for each page of the results, as a workaround - ekstrakt 2012-04-05 20:13


Problem with Tree and Table the same. setFont hovever do nothing. Tree and Table have method setItemHeight(int itemHeigh) By reflection you can invoke this method (see

For table colum you can add listener on resize cell and invoke this method by reflection (described by the link):

TableColumn column = new TableColumn(table, SWT.CENTER, i);
column.addListener(SWT.Resize, new Listener() {
    public void handleEvent(Event event) {
        int min = 10;
        setItemHeight(table, min);

min - is your minimum value for a cell height;

After that in table Listener for event SWT.MeasureItem you can set necessary height for your cells:

table.addListener(SWT.MeasureItem, new Listener() {
      public void handleEvent(Event event) {
        TableItem item = (TableItem)event.item;
                String text = item.getText(event.index);
        Point size = event.gc.textExtent(text);
        event.height = Math.max(event.height, size.y);
2012-10-12 14:35
by Alexander Drobyshevsky


I found another workaround for this bug. I wanted to resize the height of table rows, when you resize the window (table), because of manual word wrapping in the table cells. With the following listener to the SWT shell, table cells decreased properly.

shell.addListener(SWT.Resize, new Listener() {
    public void handleEvent(Event event) {
2012-07-06 11:03
by attrib
Interesting idea, I'm giving an upvote now, and I'll test it later to see if it can be applied to my table (now it's a mixture of everything (styled text, combo boxes ...) Thanks for replying after all this time : - ekstrakt 2012-07-09 22:51
Okay, this doesn't work under windows. Can't test Mac - attrib 2012-07-11 16:53


works great for dynamic row heights.

// Create tree
JTree tree = new JTree();

// All rows will be given 15 pixels of height

// Have the row height for each row computed individually

// If the row height is 0 and the height of a row has dynamically changed, it is     necessary
// to flush the internal cache of row heights. The following calls flush the     internal     cache.
if (tree.getRowHeight() <= 0) {
// Temporary change to non-zero height
2012-06-08 15:22
by Peter
That's not SWT - Edward Thomson 2012-06-08 15:37