Skip
Skip

Reputation: 6531

howto set the size of a Canvas, which doesn't have a layout

I am subclassing the Canvas in SWT, and calling pack() inside of the constructor, the Canvas' size is computed as 64x64.

There is no Style or layout inside of my class.


    public MyClass(Composite parent, int style) {
        super(parent, SWT.NONE);
        this.setBackground(getDisplay().getSystemColor(SWT.COLOR_YELLOW));

        this.computeSize(1000, 1000, true);
        this.pack();

        Point s = this.getSize();
        System.out.println(s.x); //prints 64
    }

Upvotes: 1

Views: 3276

Answers (3)

BenG
BenG

Reputation: 671

If you are subclassing Canvas then you can also override the computeSize method to force specific element sizes. This can be useful to make other elements flow around a fixed size component without having to use the layout hints, or if you want to make the canvas size set based on say an image that it's rendering.

This might be something like this in your MyCanvas class:

public Point computeSize (int widthHint, int heightHint, boolean changed) {
  Point initialSize = super.computeSize (widthHint, heightHint, changed);
  initialSize.x = 1000;
  initialSize.y = 1000;
  return initialSize;
}

Upvotes: 3

dog
dog

Reputation: 21

Have you tried hinting the desided size via GridData?

final GridData data = new GridData();
data.widthHint = 1000;
data.heightHint = 1000;
canvas.setLayoutData(data);

Upvotes: 2

Denys Séguret
Denys Séguret

Reputation: 382160

You don't give yourself the size : it is given by the layout of the component using your MyClass.

So your canvas should be ready to handle any size.

Upvotes: 2

Related Questions