I'm trying to display a website inside a JavaFX WebView component but it only renders an empty page. I've setup everything as follows:
MainController.java
public class MainController extends Application
{
private Stage primaryStage;
private Pane pane;
@Override
public void start(Stage primaryStage)
{
// ... Some proxy config here ...
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Desmon");
initRootLayout();
}
public void initRootLayout()
{
try
{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainController.class.getResource("../view/Main.fxml"));
pane = (Pane) loader.load();
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
MainViewController controller = loader.getController();
controller.setMain(this);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public Stage getPrimaryStage()
{
return primaryStage;
}
public static void main(String[] args)
{
launch(args);
}
}
MainViewController.java
public class MainViewController
{
@FXML
private WebView webTEA;
private MainController main;
public MainViewController()
{
}
@FXML
private void initialize()
{
webTEA = new WebView();
WebEngine we = webTEA.getEngine();
we.load("http://this/is/my/url.php");
}
public void setMain(MainController main)
{
this.main = main;
}
}
Main.fxml
<Pane minHeight="500.0" minWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.MainViewController">
and
<children>
<WebView fx:id="webTEA" layoutX="14.0" layoutY="14.0" prefHeight="417.0" prefWidth="570.0" />
</children>
As you can see I've also setup some proxy settings with System.setProperty()
. I also tried it without specific proxy settings - I think this is not the problem.
I don't know what I'm doing wrong. If I launch the application I can see the WebView component, but there is no content. I can only right-click and see a "Reload"-item in the context menu.
Thanks in advance.
You're creating a new WebView
instead of using the one created and injected by the FXMLLoader
. Just remove
webTEA = new WebView();
from the initialize
method to use the existing WebView
. Otherwise you initialize a new WebView
instance that is never added to the scene graph.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.