Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set max composite size in SWT

Tags:

java

eclipse

swt

Is there any way to set maximal size of composite? Only what i have found methods

setMinimumSize(Point point)
setSize(Point point)

which allow me to set minimal and prefered size.

like image 443
John Smith Avatar asked Dec 09 '22 13:12

John Smith


1 Answers

As far as I know, there is no Layout, that has a setting for maximal size, which is a good thing in my opinion.

Consider the following szenario: You set the maximal size of a Widget/Composite to a value that you think "looks good". Depending on the screen resolution and text size of the end-user, the chosen maximal size might just look wrong. This is why the layouts usually adapt to the available space.


Nevertheless, here is some code, that restricts the size:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    final Button left = new Button(shell, SWT.PUSH);
    left.setText("Restricted width");
    left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    left.addListener(SWT.Resize, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            Point size = left.getSize();

            if(size.x > 200)
            {
                left.setSize(200, size.y);
            }
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

Before resizing:

enter image description here

After resizing:

enter image description here

like image 118
Baz Avatar answered Dec 11 '22 11:12

Baz