Wednesday, March 21, 2012

Create custom browser class to embed WebView

Previous article demonstrate how to "Create web browser with JavaFX 2.0 WebView" by instancing a WebView object. It's modify to create a new class, MyBrowser, extends Region, and embed a WebView inside, such that we can easy customize our MyBrowser in the future.

Create custom class to embed WebView

package javafx_webview;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

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

private Scene scene;
MyBrowser myBrowser;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");

myBrowser = new MyBrowser();
scene = new Scene(myBrowser, 640, 480);

primaryStage.setScene(scene);
primaryStage.show();
}

class MyBrowser extends Region{

final String HOME = "http://java-buddy.blogspot.com/";
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();


public MyBrowser(){

webEngine.load(HOME);

getChildren().add(webView);
}
}
}

No comments:

Post a Comment