Creating a pop up JavaFX WebView

时间:2018-03-25 19:25:41

标签: java javafx javafx-webview

I am having some difficulty understanding how to set up a pop up window that displays a web page in my Swing application. I have seen this code across multiple tutorials but what I am struggling with is how to include this in my own code.

Every tutorial uses the main method with this code, but I want to call this from one of my other classes and I am not sure how.

This is the code in question:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;



public class WebViewMain extends Application {

public static void main(String[] args) {
    Application.launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("WebView test");

    WebView  browser = new WebView();
    WebEngine engine = browser.getEngine();
    String url = "http://zoranpavlovic.blogspot.com/";
    engine.load(url);

    StackPane sp = new StackPane();
    sp.getChildren().add(browser);

    Scene root = new Scene(sp);

    primaryStage.setScene(root);
    primaryStage.show();
}
}

I want to be able to call the WebView from an ActionListener in one of my classes like this:

JButton mapExpandBtn = new JButton();
    mapExpandBtn.setText("Expand");
    mapExpandBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            new WebViewMain();
        }
    });

This doesn't work obviously. How can I adapt my own code or the WebViewMain code to get it to launch in my application?

0 个答案:

没有答案
相关问题