Stretch javafx scene with container frame

I have compiled a minimally viable html viewer using Java. The only problem I encountered is binding the size of the browser component to the size of the JFrame. I know that I probably only missed one line of code, but today my google fu is too weak. See below for the full browser code. Currently, the browser seems to be set at a distance of about 300 pixels regardless of frame size.

Thanks Lauri

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import javax.swing.*;

public class testBrowser {

/* Start swing thread */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                showBrowser("http://www.stackoverflow.com");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public static void showBrowser(final String url) {

    // This method is invoked on Swing thread
    JFrame frame = new JFrame("FX");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    final JFXPanel fxPanel = new JFXPanel();

    frame.add(fxPanel);
    frame.setVisible(true);
    frame.pack();

    Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
        @Override
        public void run() {
            initFX(fxPanel, url);
        }
    });
}


/* Creates a WebView and fires up google.com */
private static void initFX(final JFXPanel fxPanel, String url) {
    Group group = new Group();
    final Scene scene = new Scene(group);

    WebView webView = new WebView();

    group.getChildren().add(webView);

    // Obtain the webEngine to navigate
    WebEngine webEngine = webView.getEngine();
    webEngine.load(url);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            fxPanel.setScene(scene);
        }
    });
}
}
+4
source share
1 answer

AnchorPane, , WebView 4- AnchorPane ,

//Create Layout + WebView
AnchorPane anchorPane = new AnchorPane();
WebView webView = new WebView();

//Set Layout Constraint
AnchorPane.setTopAnchor(webView, 0.0);
AnchorPane.setBottomAnchor(webView, 0.0);
AnchorPane.setLeftAnchor(webView, 0.0);
AnchorPane.setRightAnchor(webView, 0.0);

//Add WebView to AnchorPane
anchorPane.getChildren().add(webView);

//Create Scene
final Scene scene = new Scene(anchorPane);

// Obtain the webEngine to navigate
WebEngine webEngine = webView.getEngine();
webEngine.load(url);
+3

All Articles