Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.StringWriter;

import javafx.application.Application;
import javafx.concurrent.Worker.State;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);

    }

    @Override
    public void start(Stage primaryStage) {
        WebEngine webEngine = new WebEngine();
        webEngine.getLoadWorker().stateProperty().addListener((obs, oldValue, newValue) -> {
            System.out.println(newValue);
            if (newValue == State.SUCCEEDED) {
                System.out.println("finished loading");
                try {
                    TransformerFactory transformerFactory = TransformerFactory.newInstance();
                    Transformer transformer = transformerFactory.newTransformer();
                    StringWriter stringWriter = new StringWriter();
                    transformer.transform(new DOMSource(webEngine.getDocument()), new StreamResult(stringWriter));
                    String xml = stringWriter.getBuffer().toString();
                    System.out.println(xml);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }); // addListener()

        // begin loading...
        webEngine.load("http://www.java2s.com");

        Group root = new Group();
        Scene scene = new Scene(root, 300, 250);

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