2012-11-21 33 views
15

我正在與Swing應用程序混合使用JavaFX控件。在Swing JFrame上添加WebView控件

我創建了一個JavaFX控件(WebView)來瀏覽HTML文件。但我想 知道,我怎麼可以添加這個網頁視圖控制的Swing JFrame的容器?

回答

3

JFXPanel允許您將JavaFX嵌入到Swing應用程序中。

13

給定一個已經存在的jFrame,下面的代碼添加了一個新的WebView並加載網址:

// You should execute this part on the Event Dispatch Thread 
// because it modifies a Swing component 
JFXPanel jfxPanel = new JFXPanel(); 
jFrame.add(jfxPanel); 

// Creation of scene and future interactions with JFXPanel 
// should take place on the JavaFX Application Thread 
Platform.runLater(() -> { 
    WebView webView = new WebView(); 
    jfxPanel.setScene(new Scene(webView)); 
    webView.getEngine().load("http://www.stackoverflow.com/"); 
});