How to make SWT Ui responsive

I am building a GUI using SWT, I am using the Grid layout. However when I drag the window from any size and resize it leaves a load of empty space. So

I have tried using Grid Layout.

Upvotes: 0

Views: 706

Answers (1)

Shashwat
Shashwat

Reputation: 2352

Have a look in this post. Below code is from GithubGist

public class ResponsiveDemo {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display, SWT.SHELL_TRIM);
        shell.setLayout(new FillLayout());
        shell.setBounds(20, 20, 1200, 600);

        Composite parent = new Composite(shell, SWT.NONE);
        parent.setLayout(new ScaleLayout(new ScaleProvider(parent)));
        parent.setBackground(getSystemColor(SWT.COLOR_LIST_BACKGROUND));
        configure(parent);

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

    private static void configure(Composite parent) {
        ScaleData child1 = createChild(parent, "control1", SWT.COLOR_RED);
        ScaleData child2 = createChild(parent, "control2", SWT.COLOR_GREEN);
        ScaleData child3 = createChild(parent, "control3", SWT.COLOR_BLUE);
        ScaleData child4 = createChild(parent, "control4", SWT.COLOR_CYAN);
        ScaleData child5 = createChild(parent, "control5", SWT.COLOR_YELLOW);

        child1.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);
        child2.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);
        child3.on(Scale.SUPER_WIDE).setWidth(300).setMargin(3, 3, 3, 3);
        child4.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);
        child5.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);

        child1.on(Scale.WIDE).setMargin(3, 3, 3, 3);
        child2.on(Scale.WIDE).setMargin(3, 3, 3, 3);
        child3.on(Scale.WIDE).setWidth(300).setMargin(3, 3, 3, 3);
        child4.on(Scale.WIDE).tie(child5).setHeight(200).setMargin(3, 3, 3, 3);
        child5.on(Scale.WIDE).setMargin(3, 3, 3, 3);

        child1.on(Scale.STANDARD).setMargin(3, 3, 3, 3);
        child2.on(Scale.STANDARD).tie(child3).setMargin(3, 3, 3, 3);
        child3.on(Scale.STANDARD).setWidth(300).setMargin(3, 3, 3, 3);
        child4.on(Scale.STANDARD).tie(child5).setMargin(3, 3, 3, 3);
        child5.on(Scale.STANDARD).setMargin(3, 3, 3, 3);

        child1.on(Scale.COMPACT).setMargin(3, 3, 3, 3);
        child2.on(Scale.COMPACT).tie(child3).setMargin(3, 3, 3, 3);
        child3.on(Scale.COMPACT).tie(child4).setSize(300, 60).setMargin(3, 3, 3, 3);
        child4.on(Scale.COMPACT).tie(child5).setMargin(3, 3, 3, 3);
        child5.on(Scale.COMPACT).setMargin(3, 3, 3, 3);

        child1.on(Scale.SUPER_COMPACT).tie(child2).setMargin(3, 3, 3, 3);
        child2.on(Scale.SUPER_COMPACT).tie(child3).setMargin(3, 3, 3, 3);
        child3.on(Scale.SUPER_COMPACT).tie(child4).setHeight(60).setMargin(3, 3, 3, 3);
        child4.on(Scale.SUPER_COMPACT).tie(child5).setMargin(3, 3, 3, 3);
        child5.on(Scale.SUPER_COMPACT).setMargin(3, 3, 3, 3);
    }

    static ScaleData createChild(Composite parent, String label, int color) {
        return new ScaleData(createChildControl(parent, label, color));
    }

    private static Control createChildControl(Composite parent, String label, int color) {
        Label result = new Label(parent, SWT.WRAP);
        result.setText(label);
        result.setBackground(getSystemColor(color));
        return result;
    }

    private static Color getSystemColor(int colorIndex) {
        return Display.getCurrent().getSystemColor(colorIndex);
    }

}

Upvotes: 2

Related Questions