Change row height in SWT Table/Tree

Go To StackoverFlow.com

2

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
rowHeight=25;
for(Result r : results){
    if(r.hasImage()){
        rowHeight=180;
        break;
    }
}


    // 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) {
            if(event.index!=ColumnType.SCREENSHOT.toIndexNum()){
                event.detail &= ~SWT.FOREGROUND;
            }
            event.height=rowHeight;
        }
    });

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


1

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


4

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 https://bugs.eclipse.org/bugs/attachment.cgi?id=220735)

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() {
    @Override
    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


2

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() {
    @Override
    public void handleEvent(Event event) {
        table.setFont(table.getFont());
    }
});
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


-5

http://www.exampledepot.com/egs/javax.swing.tree/RowHeight.html

works great for dynamic row heights.

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

// All rows will be given 15 pixels of height
tree.setRowHeight(15);

// Have the row height for each row computed individually
tree.setRowHeight(0);

// 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
tree.setRowHeight(1);
}
tree.setRowHeight(0);
2012-06-08 15:22
by Peter
That's not SWT - Edward Thomson 2012-06-08 15:37
Ads