bellpeace bellpeace - 1 year ago 74
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);

TableColumn col = new TableColumn(table, style);
// Is this ok?

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

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

Answer Source

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);

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download