Get code from Ace Editor in JavaFX WebView

984 views Asked by At

I downloaded the Ace editor and put it in my local machine. I am using JavaFX to load it using WebView. It is very nice and all, but how do I retrieve the written code? (To a Java String)

1

There are 1 answers

0
James_D On BEST ANSWER

Just execute the Javascript editor.getValue() by calling

String code = (String)webView.getEngine().executeScript("editor.getValue()");

Complete example:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class AceEditorExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        webView.getEngine().load(getClass().getResource("Ace.html").toExternalForm());
        Button saveButton = new Button("Save");
        saveButton.setOnAction(e -> {
            String code = (String) webView.getEngine().executeScript("editor.getValue()");
            System.out.println(code);
        });

        BorderPane.setAlignment(saveButton, Pos.CENTER);
        BorderPane.setMargin(saveButton, new Insets(10));
        primaryStage.setScene(new Scene(new BorderPane(webView, null, null, saveButton, null), 800, 600));
        primaryStage.show();
    }

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

With Ace.html (for example):

<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action</title>
<style type="text/css" media="screen">
    #editor { 
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
</style>
</head>
<body>
<h2>Edit code</h2>
<div id="editor">

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class AceEditorExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        webView.getEngine().load(getClass().getResource("Ace.html").toExternalForm());
        Button saveButton = new Button("Save");
        saveButton.setOnAction(e -> {
            String code = (String) webView.getEngine().executeScript("editor.getValue()");
            System.out.println(code);
        });

        BorderPane.setAlignment(saveButton, Pos.CENTER);
        BorderPane.setMargin(saveButton, new Insets(10));
        primaryStage.setScene(new Scene(new BorderPane(webView, null, null, saveButton, null), 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
</div>

<script src="ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    var editor = ace.edit("editor");
    editor.getSession().setMode("ace/mode/java");
</script>
</body>
</html>