Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)
JxBrowser supports JavaFX toolkit and can be embedded into JavaFX desktop applications as well. To embed control that displays HTML content you must create com.teamdev.jxbrowser.chromium.javafx.BrowserView instance and put it into a Pane or any other container on Scene. (JxBrowser支持JavaFX工具包,也可以嵌入到JavaFX桌面应用程序中。要嵌入显示HTML内容的控件,必须创建com.teamdev.jxbrowser.chromium.javafx.BrowserView实例,并将其放入“窗格”或Scene中的任何其他容器中。)
Note: To use JxBrowser in JavaFX applications, JDK 1.8 or higher is required. (注意:要在JavaFX应用程序中使用JxBrowser,需要JDK 1.8或更高版本。)
The following sample demonstrates how to use JxBrowser in a simple JavaFX application:(以下样本演示了如何在简单的JavaFX应用程序中使用JxBrowser:)
import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.BrowserCore; import com.teamdev.jxbrowser.chromium.internal.Environment; import com.teamdev.jxbrowser.chromium.javafx.BrowserView; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; /** * Demonstrates how to embed Browser instance into JavaFX application. */ public class JavaFXSample extends Application { @Override public void init() throws Exception { // On Mac OS X Chromium engine must be initialized in non-UI thread. if (Environment.isMac()) { BrowserCore.initialize(); } } @Override public void start(final Stage primaryStage) { Browser browser = new Browser(); BrowserView view = new BrowserView(browser); Scene scene = new Scene(new BorderPane(view), 700, 500); primaryStage.setScene(scene); primaryStage.show(); browser.loadURL("http://www.google.com"); } public static void main(String[] args) { launch(args); } }
Note: in macOS environment you must initialize JxBrowser core functionality in non-UI thread. For example, in the Application.init() method as shown below:(注意:在macOS环境中,您必须在非UI线程中初始化JxBrowser核心功能。例如,在Application.init()方法中,如下所示:)
@Override public void init() throws Exception { // On Mac OS X Chromium engine must be initialized in non-UI thread. if (Environment.isMac()) {<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span> BrowserCore.initialize(); } }
FXML(XML文件)
It is possible to embed BrowserView into a JavaFX app GUI via FXML. In this section we will show you how to do it.
(可以通过FXML将BrowserView嵌入到JavaFX应用程序GUI中。在本节中,我们将向您展示如何进行。)
1) First we need to describe the structure of the browser-view-control.fxml file to tell JavaFX how the BrowserView control should be embedded into the app UI.(1)首先,我们需要描述browser-view-control.fxml文件的结构,以告诉JavaFX如何将BrowserView控件嵌入到应用程序UI中。)
<?xml version="1.0" encoding="UTF-8"?> <?import com.teamdev.jxbrowser.chromium.javafx.BrowserView?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.BorderPane?> <BorderPane fx:controller="BrowserViewControl" xmlns:fx="http://javafx.com/fxml"> <top> <TextField fx:id="textField" text="http://www.google.com" onAction="#loadURL"/> </top> <center> <BrowserView fx:id="browserView"/> </center> </BorderPane>
2) Then we need to implement the BrowserViewControl defined in the browser-view-control.fxml file:(2)然后,我们需要实现在browser-view-control.fxml文件中定义的BrowserViewControl:)
import com.teamdev.jxbrowser.chromium.javafx.BrowserView; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.TextField; /** * Represents FXML control with address bar and view area that * displays URL entered in the address bar text field. */ public class BrowserViewControl implements Initializable { @FXML private TextField textField; @FXML private BrowserView browserView; @Override public void initialize(URL location, ResourceBundle resources) { browserView.getBrowser().loadURL(textField.getText()); } public void loadURL(ActionEvent actionEvent) { browserView.getBrowser().loadURL(textField.getText()); } }
3) And finally, create an FXMLSample that displays the app GUI with the embedded BrowserView control using FXML:(3)最后,使用FXML创建一个FXMLSample来显示带有嵌入式BrowserView控件的应用程序GUI:)
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; /** * The sample demonstrates how to embed JavaFX BrowserView * component into JavaFX app using FXML. */ public class FXMLSample extends Application { public static void main(String[] args) { Application.launch(FXMLSample.class, args); } @Override public void start(Stage primaryStage) throws Exception { BorderPane pane = FXMLLoader.load( FXMLSample.class.getResource("browser-view-control.fxml")); primaryStage.setTitle("FXMLSample"); primaryStage.setScene(new Scene(pane, 800, 600)); primaryStage.show(); } }