简体   繁体   中英

Open new tab in swt browser after button click

I used SWT browser. I opened page and there is a Button which verify that browser have option to open new browser window. Standard SWT browser have problem with it. Above is how button is defined.

<button class="btn btn-action btn-slim size-w-90pct" data-e2e="openDealerBtn" ng-if="igDefaultRowController.account.isPdSupported" ng-class="{'btn-disabled': igDefaultRowController.shouldDisableOpenPlatformButton}" ng-disabled="igDefaultRowController.shouldDisableOpenPlatformButton" ng-click="igDefaultRowController.openDealer()" ig-click-tracking="pureDealBtn-CFD" id="openDealerButton-XQ7JI"> <span class="btn-label" ig-i18n="" key="AccountOverview.openDealer"><span ng-bind-html="value">Open classic platform</span></span> </button>
  1. [SOLVED!] How to expand SWT browser to open more than one tab ?
    • I used TabFolder for more tabs.
  2. It is possible to catch URL after click on this button and open in new SWT browser tab ?

SWT uses one of the browsers that are available on the operating system and embeds the main "view" of the browser (the bit that displays the html) in your application. That does mean, however, that it doesn't come with all the fancy stuff like tabs.

As you already discovered yourself, you can get around this by using a TabFolder .

The question now is: how do you know when a tab should be opened. This code (adopted from Snippet270 ) should help you with this:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Main Window");
    shell.setLayout(new FillLayout());
    final Browser browser;
    try
    {
        browser = new Browser(shell, SWT.NONE);
    }
    catch (SWTError e)
    {
        System.out.println("Could not instantiate Browser: " + e.getMessage());
        display.dispose();
        return;
    }
    initialize(display, browser);
    shell.open();
    browser.setUrl("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_links_target");
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

/* register WindowEvent listeners */
static void initialize(final Display display, Browser browser)
{
    browser.addOpenWindowListener(e ->
    {
        Shell shell = new Shell(display);
        shell.setText("New Window");
        shell.setLayout(new FillLayout());
        Browser browser1 = new Browser(shell, SWT.NONE);
        initialize(display, browser1);
        e.browser = browser1;
    });
    browser.addVisibilityWindowListener(new VisibilityWindowListener()
    {
        @Override
        public void hide(WindowEvent e)
        {
            Browser browser = (Browser) e.widget;
            Shell shell = browser.getShell();
            shell.setVisible(false);
        }

        @Override
        public void show(WindowEvent e)
        {
            Browser browser = (Browser) e.widget;
            final Shell shell = browser.getShell();
            if (e.location != null) shell.setLocation(e.location);
            if (e.size != null)
            {
                Point size = e.size;
                shell.setSize(shell.computeSize(size.x, size.y));
            }
            shell.open();
        }
    });
    browser.addCloseWindowListener(e ->
    {
        Browser browser1 = (Browser) e.widget;
        Shell shell = browser1.getShell();
        shell.close();
    });
}

This will open the link in a new Shell with a new Browser . You can change this, so it creates a new tab and adds the new browser to the new tab.


EDIT

Here's a working example using TabFolder :

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

    TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);

    addNewBrowser(tabFolder, "<a href='http://www.google.co.uk' target='_blank'>Click here!</a>");

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

private static Browser addNewBrowser(TabFolder folder, String html)
{
    TabItem item = new TabItem(folder, SWT.NONE);
    Composite c = new Composite(folder, SWT.NONE);
    item.setControl(c);
    c.setLayout(new FillLayout());

    Browser browser = new Browser(c, SWT.NONE);

    if (html != null)
    {
        browser.setText(html);
        item.setText("Original tab");
    }
    else
    {
        item.setText("New tab");
    }

    browser.addOpenWindowListener(e ->
    {
        e.browser = addNewBrowser(folder, null);
    });
    browser.addVisibilityWindowListener(new VisibilityWindowListener()
    {
        @Override
        public void hide(WindowEvent e)
        {
            Browser browser = (Browser) e.widget;
            Shell shell = browser.getShell();
            shell.setVisible(false);
        }

        @Override
        public void show(WindowEvent e)
        {
            Browser browser = (Browser) e.widget;
            final Shell shell = browser.getShell();
            if (e.location != null) shell.setLocation(e.location);
            if (e.size != null)
            {
                Point size = e.size;
                shell.setSize(shell.computeSize(size.x, size.y));
            }
            shell.open();
        }
    });
    browser.addCloseWindowListener(e ->
    {
        Browser browser1 = (Browser) e.widget;
        Shell shell = browser1.getShell();
        shell.close();
    });

    folder.setSelection(item);

    return browser;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM