Reputation: 1425
I'm a beginner at JavaFX.
I've made my layout in JavaFX Scene Builder. In that I've left a WebView but I cannot figure how to give it a URL.
In a separate class I've my WebView ready
public class GoogleApp 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("Railway Scenario Development");
myBrowser = new MyBrowser();
scene = new Scene(myBrowser, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
class MyBrowser extends Region{
HBox toolbar;
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
public MyBrowser(){
final URL urlGoogleMaps = getClass().getResource("demo.html");
webEngine.load(urlGoogleMaps.toExternalForm());
getChildren().add(webView);
}
}
}
But I need to embed that in my application which I've made in Scene Builder. In Scene Builder there is a WebView Panel and I want that panel to look like the code I've mentioned above.
My application's code uses the fxml file as its "scene" (Shown below).
public class Railway extends Application {
private Scene scene;
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.setTitle("Railway Scenario Development");
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I figure I've to do something in the FXMLDocumentController file. But how and what. Please guide.
Upvotes: 2
Views: 8904
Reputation: 25
I found a way to your first Version.
May others could try this: http://java-buddy.blogspot.de/2012/05/embed-webview-in-fxml-to-load.html.
It worked for me.
Here is an Example FXML file.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafxml_web.*?><!--- Important Use your own Package instead of "javafxml_web.*" --->
<AnchorPane id="AnchorPane" prefHeight="500" prefWidth="660" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxml_web.Sample">
<children>
<MyBrowser id="mybrowser" layoutX="10" layoutY="10" prefHeight="460" prefWidth="620" fx:id="mybrowser" />
</children>
</AnchorPane>
And in your Controllerclass you have to use:
@FXML
private MyBrowser mybrowser;
This should solve your Issues.
Upvotes: 0
Reputation: 3126
now its work perfectly...try this
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class FXMLDocumentController {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private StackPane root;
@FXML
void initialize() {
assert root != null : "fx:id=\"root\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
WebView view = new WebView();
WebEngine engine = view.getEngine();
engine.load("http://www.google.com/");
root.getChildren().add(view);
}
}
Upvotes: 1