Last active
March 10, 2019 12:20
-
-
Save ezhov-da/f93bb6c46ffbb0a484967add8050f698 to your computer and use it in GitHub Desktop.
javafx web view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[code:]java[:code] | |
package testfx; | |
import java.util.logging.Logger; | |
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.ScrollPane; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.web.WebEngine; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
import netscape.javascript.JSObject; | |
/** | |
* | |
* @author ezhov_da | |
*/ | |
public class WebViewTest extends Application | |
{ | |
private static final Logger LOG = Logger.getLogger(WebViewTest.class.getName()); | |
private String url = "file:///C:/Users/ezhov_da/Desktop/index.htm"; | |
private TextArea textArea = new TextArea(); | |
public static void main(String[] args) | |
{ | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception | |
{ | |
WebView webView = new WebView(); | |
WebEngine webEngine = webView.getEngine(); | |
webEngine.load(url); | |
BorderPane borderPane = new BorderPane(webView); | |
ScrollPane scrollPane = new ScrollPane(textArea); | |
scrollPane.setFitToHeight(true); | |
scrollPane.setFitToWidth(true); | |
//JSObject jsobj = (JSObject) webEngine.executeScript("window"); | |
//jsobj.setMember("java", new Bridge()); | |
textArea.textProperty().addListener(new ChangeListener<String>() | |
{ | |
@Override | |
public void changed(final ObservableValue<? extends String> observable, final String oldValue, final String newValue) | |
{ | |
if (newValue != null && !"".equals(newValue)) | |
{ | |
String val = newValue; | |
val = val.replace("'", "\\'"); | |
val = val.replace("\"'", "\\"); | |
val = val.replace(System.getProperty("line.separator"), "\\n"); | |
val = val.replace("\n", "\\n"); | |
val = val.replace("\r", "\\n"); | |
webEngine.executeScript("textFrom = \"" + val + "\""); | |
webEngine.executeScript("editor.setValue(textFrom, -1);"); | |
} | |
} | |
}); | |
VBox vBox = new VBox(); | |
vBox.getChildren().addAll(borderPane, scrollPane); | |
Scene scene = new Scene(vBox); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
class Bridge | |
{ | |
public void setText() | |
{ | |
textArea.setText("asaszbzfnxdnxnxcvnxvn"); | |
} | |
} | |
} | |
[/code] | |
[code:]html[:code] | |
<!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> | |
<script> | |
$( document ).ready(function() { | |
editor.on('change', function() { | |
//java.setText() | |
//alert("i'm alive"); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="editor"></div> | |
<script src="ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script> | |
<script> | |
var textFrom = "" | |
var editor = ace.edit("editor"); | |
editor.setTheme("ace/theme/monokai"); | |
editor.getSession().setMode("ace/mode/groovy"); | |
</script> | |
</body> | |
</html> | |
[/code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment