Using the FormToolkit, how do I set a border on the swt Browser widget?

1.3k views Asked by At

Using the eclipse "FormToolkit", how do I set a border on the swt Browser widget?

This is the way how to create a Composite with border:

Composite composite = toolkit.createComposite(parent, SWT.BORDER);

There is no createBrowser() in the FormToolkit so this is how to create one:

Browser browser = new Browser(parent, SWT.BORDER);
toolkit.adapt(browser);

The problem is that this does not paint a border for the browser, even after calling:

toolkit.paintBordersFor(browser);

So how do I to create a border for the browser?

Here is a test application:

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Shell");
    shell.setSize(250, 250);
    shell.setLayout(new FillLayout());

    Composite parent = new Composite(shell, SWT.NONE);
    parent.setLayout(new GridLayout(2, false));

    GridData compositeGD = new GridData();
    compositeGD.widthHint = 100;
    compositeGD.heightHint = 100;

    FormToolkit toolkit = new FormToolkit(display);
    Composite composite = toolkit.createComposite(parent, SWT.BORDER);
    composite.setLayoutData(compositeGD);

    GridData browserGD = new GridData();
    browserGD.widthHint = 100;
    browserGD.heightHint = 100;

    Browser browser = new Browser(parent, SWT.BORDER);
    toolkit.adapt(browser);
    toolkit.paintBordersFor(browser);
    browser.setLayoutData(browserGD);

    browser.setText("<html><body>CONTENT</body></html>");

    shell.open();

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

There are 2 answers

0
flavio.donze On

I'm not quite sure if this is the right explanation but this is what I came up with.
You have to create the border using HTML and pass it to the browser.
Maybe because if you have a border in the HTML you would have a double border with the Browser border.

This was your content:

<html><body>CONTENT</body></html>

Change it to something like:

<html><body style="border: 1px solid gray; margin: 0px; padding: 5px;">CONTENT</body></html>

The Test application:

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Shell");
    shell.setSize(250, 250);
    shell.setLayout(new FillLayout());

    Composite parent = new Composite(shell, SWT.NONE);
    parent.setLayout(new GridLayout(2, false));

    GridData compositeGD = new GridData();
    compositeGD.widthHint = 100;
    compositeGD.heightHint = 100;

    FormToolkit toolkit = new FormToolkit(display);
    Composite composite = toolkit.createComposite(parent, SWT.BORDER);
    composite.setLayoutData(compositeGD);

    GridData browserGD = new GridData();
    browserGD.widthHint = 100;
    browserGD.heightHint = 100;

    Browser browser = new Browser(parent, SWT.BORDER);
    toolkit.adapt(browser);
    toolkit.paintBordersFor(browser);
    browser.setLayoutData(browserGD);

    browser.setText("<html><body style=\"border: 1px solid #ABADB3; margin: 0px; padding: 5px;\">CONTENT</body></html>");

    shell.open();

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

Before: Printscreen without border After: Printscreen with border

0
Tom Seidel On

To have a consistent Look & Feel of Composite and Browser-Widgets you should wrap your browser widget into a composite, something like

final FormToolkit toolkit = new FormToolkit(display);
final Composite composite = toolkit.createComposite(parent, SWT.BORDER);
composite.setLayoutData(gridData);

final Composite composite2 = toolkit.createComposite(parent, SWT.BORDER);
composite2.setLayoutData(gridData);
final FillLayout layout = new FillLayout();

composite2.setLayout(layout);

final Browser browser = new Browser(composite2, SWT.NONE);
toolkit.adapt(browser);

browser.setText("<html><body>CONTENT</body></html>");

enter image description here