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?

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
adesh
  • 1,117
  • 3
  • 18
  • 28

2 Answers2

31

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/");
});
heenenee
  • 19,367
  • 1
  • 54
  • 83
5

JFXPanel allows you to embed JavaFX within your Swing application.

Emily Crutcher
  • 628
  • 5
  • 10