I am attempting to determine the size of a SWT view so I can layout the widgets correctly in a plugin. I am running on Eclipse Neon with Java 8.
The code I am using follows:
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
public class ClockView extends ViewPart {
/**
* The constructor.
*/
public ClockView() {
}
/**
* This is a callback that will allow us
* to create the viewer and initialize it.
*/
@Override
public void createPartControl(Composite parent) {
final RowLayout layout = new RowLayout(SWT.HORIZONTAL);
layout.center = true;
layout.justify = true;
final Point area = parent.getSize();
final ImmutableList<Integer> clockWidths = ImmutableList.<Integer>of(100, 200, 400);
layout.marginWidth = (area.x - Iterables.getLast(clockWidths, null)) / 2;
layout.marginHeight = (area.y - Iterables.getLast(clockWidths, null)) / 2;
parent.setLayout(layout);
clockWidths.forEach(width -> {
ClockWidget c = new ClockWidget(parent, SWT.NONE);
c.setLayoutData(new RowData(width, width));
});
}
/**
* Passing the focus request to the viewer's control.
*/
@Override
public void setFocus() {
}
}
My problem is that every method I tried returns 0, 0 for the size of the view. I have tried scrollable.getClientArea and Composite.getSize.
How can I determine the area available to work with?