Add WebView control on Swing JFrame
Asked Answered
P

2

22

I am working on Swing application mixed with JavaFX control.

I have created a JavaFX control (WebView) to browse HTML files. But I want to know, how can I add this web view control on the container of a Swing JFrame?

Pleiades answered 21/11, 2012 at 6:46 Comment(0)
K
32

Given an already existing jFrame, the following code adds a new WebView and loads a URL:

// 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/");
});
Kike answered 28/7, 2015 at 3:14 Comment(1)
Is there a way to keep a pointer to the webView element in Swing?Garver
C
5

JFXPanel allows you to embed JavaFX within your Swing application.

Caelum answered 9/2, 2015 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.