Mridang Agarwalla
Mridang Agarwalla

Reputation: 45108

Preventing a SWT window from being resized horizontally

Is there a STYLE bit that I could apply to my shell/dialog to prevent horizontal resizing? I would like the window to allow vertical resize only.

Thanks.

Upvotes: 5

Views: 2274

Answers (1)

user452425
user452425

Reputation:

Use the ControlListener to capture the resize event of the window. If user try to change width of the window restore the default value of it.

import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestWindow {

    private static int CONST_WIDTH = 200;

    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setBounds(shell.getBounds().x, shell.getBounds().y, CONST_WIDTH, shell.getBounds().height);
        shell.setText("Test");

        shell.addControlListener(new ControlAdapter() {

            @Override
            public void controlResized(ControlEvent e) {
                Rectangle rect = shell.getBounds();
                if(rect.width != CONST_WIDTH) {
                    shell.setBounds(rect.x, rect.y, CONST_WIDTH, rect.height);
                }
            }
        });

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

Upvotes: 6

Related Questions