JavaFX webView blank showing window - javafx

How my webview is empty ? Why I have a blank window ?
Here my start method
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.setFill(Color.BLUE);
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
//webEngine.load("https://www.google.fr");
webEngine.loadContent("<p>qqqqqqq</p>");
primaryStage.setScene(scene);
primaryStage.show();

You don't add the WebView as a node in the scene.
root.setCenter(webView);

Related

JavaFX WebView contextmenu unavailable

I have setContextMenuEnabled(true), but it doesn't work, no context menu is available!
FXCanvas canvas = new FXCanvas(composite, SWT.EMBEDDED);
StackPane root = new StackPane();
webView = new WebView();
webView.setContextMenuEnabled(true);
root.getChildren().add(webView);
Scene scene = new Scene(root);
canvas.setScene(scene);
Can anyone help?

JAVAFX: How can I put this into one window instead of two?

I've been trying to make a toolbar inside a window with a checkers game, what happens now is, Checkers game opening in a separate window and so is the toolbar, what am I doing wrong? How can I make this code open in one window with both functions?
#Override
public void start(Stage primaryStage) throws Exception {
Stage toolStage = new Stage();
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
Scene toolScene = new Scene(pane, 600, 400);
toolStage.setScene(toolScene);
toolStage.show();
Scene scene = new Scene(createContent());
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}
You're creating a new Scene+Stage fot the toolbar, show it and then show the content in the primaryStage instead of adding both toolbar and content as parts of the same scene, e.g. by adding the content as center node of the BorderPane:
#Override
public void start(Stage primaryStage) throws Exception {
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
pane.setCenter(createContent());
Scene scene = new Scene(pane, 600, 400);
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}

Start html file from pane in javafx

In my JavaFX implementation I use a pane, where I want to open a webview, when a button on the pane is clicked.
But however with this snippet it does not work:
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.load("http://google.com");
button.setOnAction((event) -> {
webEngine.load(url.toExternalForm());
});
I've got this to call fxml:
Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
Any hint? thx
You have to add your pane to the root and add your webview to the pane. Also, look at this button handler compared to yours.
Button btn = new Button();
btn.setText("Say 'Hello World'");
WebView webview = new WebView();
WebEngine webengine = webview.getEngine();
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
webengine.load("http://www.google.com");
}
});
VBox root = new VBox();
root.getChildren().addAll(btn, webview);//I don't see where you are doing this
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();

HBox: fit to parent AnchorPane Javafx, not using FXML

How can we set an hbox fit to its parent AnchorPane as we do in FXML sceneBuilder.
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root, 700, 500);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
//MenuBar
MenuBar menuBar=new MenuBar();
Menu file=new Menu("File");
MenuItem loadStudentData=new MenuItem("Load Student Data Ctrl+L");
MenuItem saveStudentData=new MenuItem("Save Student Data Ctrl+S");
MenuItem exit=new MenuItem("Exit Ctrl+X");
Menu help=new Menu("Help");
MenuItem about=new MenuItem("About");
file.getItems().add(loadStudentData);
file.getItems().add(saveStudentData);
file.getItems().add(exit);
help.getItems().add(about);
menuBar.getMenus().addAll(file);
menuBar.getMenus().addAll(help);
HBox hboxMenu=new HBox();
hboxMenu.getChildren().add(menuBar);
HBox.setHgrow(menuBar, Priority.ALWAYS);
root.getChildren().add(hboxMenu);
primaryStage.setScene(scene);
primaryStage.show();
I want menuBar to get auto resize when window is resized.
You have to set the AnchorPane constraints for hbox:
AnchorPane.setLeftAnchor(hboxMenu, 0d);
AnchorPane.setRightAnchor(hboxMenu, 0d);
(But the prefered way to use a MenuBar is putting it into the top of a BorderPane)

Closing a scene in javafx

I'm learning javafx and I have a problem.
I'm building an interface with a border pane and a menu bar at the top and when I click on the items I want scenes to be loaded on the center of the border pane. That seems to be working alright. I want to add a button to close the scene but I can't make it work.
See below the code.
stage = primaryStage;
stage.setTitle("My Program");
BorderPane pane = new BorderPane();
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
MenuItem load = new MenuItem("Load");
MenuItem save = new MenuItem("Save");
....
BorderPane sp = new BorderPane();
sp.setStyle("-fx-background: #FF0000;");
Button btn = new Button("Close");
btn.setPrefSize(200, 20);
btn.setLayoutX(200);
btn.setLayoutY(200);
sp.getChildren().add(btn);
load.setOnAction(e -> pane.setCenter(sp));
btn.setOnAction(e -> ???????????????????);
scene1 = new Scene(sp);
scene = new Scene(pane, 800, 500);
stage.setScene(scene);
stage.show();
I was wondering if it's something that I can do and what code I should put instead of the question marks.
Any help is appreciated. Thanks in advance.
btn.setOnAction(e -> pane.setCenter(null));
to clear the border pane content, or
btn.setOnAction(e -> stage.hide());
to close the entire window

Resources