Antoine Duval
Antoine Duval

Reputation: 342

How to make a Button on a JavaFX's browser?

I'm trying to make a "launcher" with javafx.
This is my code :
I'm not shure you have to read all of this code, this code is here.
I'm trying to put a javaFX "play" button (i know how to make a button and how to set up an onclick event but i don't know where to add it :/)
Have you got an idea ? Thx.

package fr.whiteplay.main.launcher;

public class Launcher{

    private static WebViewSample launcher;
    private static String[] a;

    public static void main(String[] args){
        launcher = new WebViewSample();
        a = args;
    }

    public static void start(){
        launcher.go(a);
    }
}


package fr.whiteplay.main.launcher;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

public class WebViewSample extends Application{

    private Browser browser;
    private Scene scene;

    public void start(Stage stage){
        // create the scene
        stage.setTitle("WhitePlay");
        browser = new Browser();
        scene = new Scene(browser, 992, 620, Color.web("#000000"));
        stage.setScene(scene);
        stage.show();
    }

    public static void go(String[] args){
        launch(args);
    }
}


package fr.whiteplay.main.launcher;

import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

class Browser extends Region{

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

    public Browser(){
        getStyleClass().add("browser");
        webEngine.load("http://www.whiteplay.fr/launcher/index.html");
        getChildren().add(browser);
    }

    private Node createSpacer(){
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    protected void layoutChildren(){
        layoutInArea(browser, 0, 0, getWidth(), getHeight(), 0, HPos.CENTER, VPos.CENTER);
    }
}

Upvotes: 0

Views: 835

Answers (1)

Jan X Marek
Jan X Marek

Reputation: 2504

Instead of the browser itself, the scene root must be a structured panel, which contains the browser, the button, and whatever else.

The simplest example is to replace your WebViewSample.start() method with the following:

public void start(Stage stage){
    // create the scene
    stage.setTitle("WhitePlay");
    browser = new Browser();
    BorderPane root = new BorderPane();
    root.setCenter(browser);
    Button button = new Button("Play");
    root.setBottom(button);
    button.setOnAction(a -> System.out.println("Play"));
    scene = new Scene(root, 992, 620, Color.web("#000000"));
    stage.setScene(scene);
    stage.show();
}

Check this page for further reference on various layouts options, and how to work with them.

Upvotes: 1

Related Questions