单击按钮后在swt浏览器中打开新选项卡

时间:2016-09-22 13:43:12

标签: java browser swt

我使用了SWT浏览器。我打开了页面,有Button验证浏览器是否有选项可以打开新的浏览器窗口。标准SWT浏览器存在问题。以上是按钮的定义方式。

<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. [已解决!]如何展开SWT浏览器以打开多个标签?
    • 我使用TabFolder获取更多标签。
  2. 点击此按钮后可以捕获URL并在新的SWT浏览器选项卡中打开?

1 个答案:

答案 0 :(得分:1)

SWT使用操作系统上可用的浏览器之一,并在应用程序中嵌入浏览器的主“视图”(显示html的位)。然而,这确实意味着它并没有带有像制表符这样的奇特东西。

正如您已经发现的那样,您可以使用TabFolder来解决这个问题。

现在的问题是:你怎么知道何时应该打开一个标签。此代码(从Snippet270采用)可以帮助您:

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();
    });
}

这将在新的Shell中打开一个新的Browser链接。您可以更改它,因此它会创建一个新选项卡并将新浏览器添加到新选项卡。

修改

以下是使用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;
}