Thinhbk
Thinhbk

Reputation: 2214

Cannot display web page on WebView control

I tried to follow the instruction describe here: http://docs.oracle.com/javafx/2.0/webview/jfxpub-webview.htm to display a web page on javafx 2.1, but web page does not display although I've already signed my application using javafx-ant tool (this link mentions about signing javafx application How can I display a javascript webpage inside a WebView in JavaFx 2.0 in Browser).

I can view the web page when running javafx on browser, but can't view it when running application as java stand alone application.

Here's the code:

browser control:

public class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        //apply the styles
        getStyleClass().add("browser");
        // load the web page
        webEngine.load("http://www.google.com");
        //add the web view to the scene
        getChildren().add(browser);


    }
    //...
}

Display it:

  Stage stage = new Stage();

    stage.setTitle("Web View");
    Scene scene = new Scene(new Browser(),590,400, Color.web("#666970"));
    stage.setScene(scene);
    scene.getStylesheets().add("/com/sai/javafx/calendar/styles/BrowserToolbar.css");  

  stage.show();

Any idea would be highly appreciated.

Upvotes: 1

Views: 4010

Answers (2)

Thinhbk
Thinhbk

Reputation: 2214

As far as I know, the only way to solve my problem is loading the .html file locally (can't load web page from other domain, and this html file must not refer to any external link)

public Browser() {
   //apply the styles
   getStyleClass().add("browser");        
   // load the web page
   final URL urlHello = Browser.class.getResource("sample.html");
   webEngine.load(urlHello.toExternalForm());
}

Display it:

Stage stage = new Stage();

stage.setTitle("Web View");
Scene scene = new Scene(new Browser(),590,400, Color.web("#666970"));
stage.setScene(scene);
scene.getStylesheets().add("/com/sai/javafx/calendar/styles/BrowserToolbar.css");  

stage.show();

Upvotes: 0

jewelsea
jewelsea

Reputation: 159416

Here is a sample app:

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

public class WebViewSample extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(final Stage primaryStage) {
    final WebView webView = new WebView(); 
    webView.getEngine().load("http://docs.oracle.com/javafx/");
    primaryStage.setScene(new Scene(webView));
    primaryStage.show();
  }
}

Here is what it looks like in a browser, the inner scrollpane is the JavaFX WebView rendered browser and the outer text is the html page embedding the JavaFX WebView.

WebView in Browser Sample Image

To run it I used NetBeans nightly build, JavaFX 2.2b4, JDK7u6ea, Win7, IE9.

Steps in NetBeans were:

  1. New Project | JavaFX | JavaFX Application
  2. Create a Java file with the sample app code.
  3. After project created, right click Project | Properties
    • Build | Deployment | Check "Request Unrestricted Access"
    • Run | Check radio Run "in Browser"
    • OK to accept property changes
  4. Press F6 to run the application in the browser.
  5. Accept any security dialog warnings.
  6. Wait a few seconds for the WebView to load.

Upvotes: 2

Related Questions