Friday, March 22, 2013

Embed Youtube video in JavaFX WebView

The code embed Youtube view in JavaFX WebView.

Embed Youtube video in JavaFX WebView
Embed Youtube video in JavaFX WebView


package javafx_webviewyoutube;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_WebViewYoutube extends Application {

String content_Url = "<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/9bZkp7q19f0\" frameborder=\"0\" allowfullscreen></iframe>";

@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.loadContent(content_Url);

StackPane root = new StackPane();
root.getChildren().add(webView);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("http://java-buddy.blogspot.com/");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

To load the html code from YouTube, open a browser to load Youtube with the target video, select Share -> Embed to copy the html code into content_Url.


No comments:

Post a Comment