bellpeace bellpeace - 15 days ago 5
Java Question

Java Table weirdness on Mac

I am developing an Eclipse plugin that uses Table to print out to a view. I tested the system on various Eclipse and Ubuntu versions and the values in the table are visible. i.e. rows fit the content. But when deploying it to Mac, the rows do not fit the content. I can see that something is inside but the whole content is not visible nor I can resize it. Here is the code I am using:

int style = SWT.NONE;
Table table = new Table(composite, style);
table.setHeaderVisible(true);
table.setLinesVisible(true);

TableColumn col = new TableColumn(table, style);
// Is this ok?
col.setWidth(200);
col.setResizable(true);

for (int i = 0; i < contents.size(); i++){
TableItem item = new TableItem(table, style);
item.setText(contents.get(i));
}


I tried with different styles but no luck. Any idea what is going wrong?

Answer

Ok, I came up with this piece of code that does the trick:

public class TableItemPaintListener implements Listener{    
 public void handleEvent(Event event) {
     switch (event.type) {
              case SWT.MeasureItem: {
                  TableItem item = (TableItem) event.item;
                  String text = item.getText();
                  Point size = event.gc.textExtent(text);
                  event.width = size.x;
                  event.height = Math.max(event.height, size.y);
                  break;
            }    
         }
     }
}

and then:

table.addListener(SWT.MeasureItem, new TableItemPaintListener()); 

Basically, we install a handler on a paint event that is triggered on each table item. Then, we set the cell height value to fit the content.