2

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?

Jonathan
  • 2,635
  • 3
  • 30
  • 49
  • 2
    Sizes haven't been determined when `createPartControl` is called. There will be a Resize event when the size is set. – greg-449 Jul 13 '18 at 13:23
  • @gregg-449 Make that an answer and I can accept it. I am just learning SWT and hence the question. – Jonathan Jul 13 '18 at 13:32

1 Answers1

4

Sizes haven't been determined when createPartControl is called.

There will be a Resize event when the size is set.

Use getClientArea() on the main Composite to get the size of the view area.

greg-449
  • 109,219
  • 232
  • 102
  • 145