JFXTextField不能与JxBrowser

时间:2017-02-22 02:00:37

标签: java javafx jxbrowser

我有点紧张。 我在我的JavaFX项目中使用JxBrowser。 我也用JFoenix来推动外观。 现在,当我浏览一些站点时,作为JFXTextField的URLBar变得不可用。我不能再写了。 但默认的JavaFX上下文菜单仍然有效。 有趣的是,网站上的主要听众,如F在youtube上的全屏仍然有效。 要解决这个问题,唯一可行的方法是在PC上单击一个完全不同的窗口。 例如,我点击IntelliJ而不是回到JavaFX框架,而不是再次工作。正如我在specefic网站上写的那样。 没有异常被抛出。

如果它很重要,我使用2个TabPanes,一个用于Tab,另一个用于内容。

有人有想法吗?

生成Tab的类:

package de.liz3.liz3web.browser;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import de.liz3.liz3web.gui.controller.MainController;
import javafx.application.Platform;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.KeyCode;

import java.util.Vector;

/**
* Created by Liz3 on 22.02.2017.
*/
public class TabManager {

private Vector<BrowserTab> activeTabs;
private MainController controller;

public TabManager(MainController controller) {
    this.controller = controller;

    activeTabs = new Vector<>();

    controller.getUrlField().setOnMouseClicked(event -> controller.getUrlField().requestFocus());
    TabPane head = controller.getHeaderTabPane();
    controller.getUrlField().setOnKeyReleased(event -> {

        if (event.getCode() == KeyCode.ENTER) {

            selectedBrowserTab().browseOrSearch(TabManager.this.controller.getUrlField().getText());
        }
    });
    controller.getBackBtn().setOnAction(event -> selectedBrowserTab().getEngine().goBack());
    controller.getForwardBtn().setOnAction(event -> selectedBrowserTab().getEngine().goForward());
    head.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

        if (newValue == controller.getNewTab()) {
            newTab(null);
            return;
        }
        controller.getMainTabPane().getSelectionModel().select(getContentTab(newValue));

        controller.getUrlField().setText(selectedBrowserTab().getCurrentUrl());
    });
}

public void newTab(String url) {


    new Thread(() -> {

        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        Tab headerTab = new Tab("New Tab");
        Tab contentTab = new Tab();
        contentTab.setContent(view);
        BrowserTab t = new BrowserTab(browser, view, contentTab, headerTab);
        activeTabs.add(t);


        Platform.runLater(() -> {

            controller.getHeaderTabPane().getTabs().add(headerTab);
            controller.getMainTabPane().getTabs().add(contentTab);
            controller.getHeaderTabPane().getTabs().remove(controller.getNewTab());
            controller.getHeaderTabPane().getTabs().add(controller.getNewTab());
            controller.getMainTabPane().getSelectionModel().select(contentTab);
            controller.getHeaderTabPane().getSelectionModel().select(headerTab);
            controller.getUrlField().setEditable(true);
            controller.getUrlField().setText("");
            t.browseOrSearch(url);

        });
    }).start();

}


public BrowserTab selectedBrowserTab() {

    for (BrowserTab tab : activeTabs) {

        if (tab.getHeaderTab() == controller.getHeaderTabPane().getSelectionModel().getSelectedItem()) {
            return tab;
        }
    }
    return null;
}

public Tab getSelectedHeaderTab() {

    return controller.getHeaderTabPane().getSelectionModel().getSelectedItem();
}

public Tab getSelectedContentTab() {

    return controller.getMainTabPane().getSelectionModel().getSelectedItem();
}

public Tab getHeaderTab(Tab contentTab) {

    for (BrowserTab tab : activeTabs) {

        if (tab.getContentTab() == contentTab) {
            return tab.getHeaderTab();
        }
    }
    return null;
}

public Tab getContentTab(Tab headerTab) {

    for (BrowserTab tab : activeTabs) {

        if (tab.getHeaderTab() == headerTab) {
            return tab.getContentTab();
        }
    }
    return null;
}

public MainController getController() {
    return controller;
}

public Vector<BrowserTab> getActiveTabs() {
    return activeTabs;
}
}

浏览器类:

package de.liz3.liz3web.browser;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.events.*;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import de.liz3.liz3web.Main;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Tab;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Liz3 on 21.02.2017.
 */
public class BrowserTab {

private Browser engine;
private BrowserView view;
private Tab contentTab;
private Tab headerTab;
private String currentUrl;



public BrowserTab(Browser engine, BrowserView view, Tab contentTab, Tab headerTab) {
    this.engine = engine;
    this.view = view;
    this.contentTab = contentTab;
    this.headerTab = headerTab;
    this.currentUrl = "";

    setUpTab();

}

public void browseOrSearch(String address) {

    if (address == null) {
        return;
    }

    if (address.startsWith("http://") || address.startsWith("https://")) {

        this.engine.loadURL(address);

        return;
    }
    if (!address.contains(" ") && address.contains(".")) {

        address = "http://" + address;
        this.engine.loadURL(address);
        return;
    }

    String encode = null;
    try {
        encode = URLEncoder.encode(address, "utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    this.engine.loadURL("https//google.com/search?q=" + encode);

}

private void setUpTab() {

    this.headerTab.setClosable(true);
    this.headerTab.setOnCloseRequest(new EventHandler<Event>() {
        @Override
        public void handle(Event event) {

            Main.tm.getController().getMainTabPane().getTabs().remove(contentTab);
            Main.tm.getActiveTabs().remove(this);

        }
    });
  this.engine.addTitleListener(titleEvent -> Platform.runLater(() -> BrowserTab.this.headerTab.setText(titleEvent.getTitle())));
    this.engine.addLoadListener(new LoadListener() {
        @Override
        public void onStartLoadingFrame(StartLoadingEvent startLoadingEvent) {

            Platform.runLater(() -> {
                BrowserTab.this.currentUrl = BrowserTab.this.engine.getURL();
                if (Main.tm.getSelectedHeaderTab() == BrowserTab.this.headerTab) {

                    Main.tm.getController().getUrlField().setText(BrowserTab.this.currentUrl);
                }
            });

        }

        @Override
        public void onProvisionalLoadingFrame(ProvisionalLoadingEvent provisionalLoadingEvent) {

        }

        @Override
        public void onFinishLoadingFrame(FinishLoadingEvent finishLoadingEvent) {
            Platform.runLater(() -> {

                BrowserTab.this.currentUrl = BrowserTab.this.engine.getURL();
                if (Main.tm.getSelectedHeaderTab() == BrowserTab.this.headerTab) {

                    Main.tm.getController().getUrlField().setText(BrowserTab.this.currentUrl);
                }
            });
        }

        @Override
        public void onFailLoadingFrame(FailLoadingEvent failLoadingEvent) {

        }

        @Override
        public void onDocumentLoadedInFrame(FrameLoadEvent frameLoadEvent) {

        }

        @Override
        public void onDocumentLoadedInMainFrame(LoadEvent loadEvent) {

        }
    });
    this.engine.setPopupHandler(popupParams -> {
        Platform.runLater(() -> Main.tm.newTab(popupParams.getURL()));

        return null;
    });

}

public Browser getEngine() {
    return engine;
}

public BrowserView getView() {
    return view;
}

public Tab getContentTab() {
    return contentTab;
}

public Tab getHeaderTab() {
    return headerTab;
}

public String getCurrentUrl() {
    return currentUrl;
}
}

1 个答案:

答案 0 :(得分:1)

默认情况下,JxBrowser正在HEAVYWEIGHT渲染模式下运行。不建议在具有标签和图层的应用程序中使用HEAVYWEIGHT渲染模式。请使用LIGHTWEIGHT渲染模式。它应该没有任何问题:

Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
BrowserView view = new BrowserView(browser);