1

as said in topic: JavaFX HTMLEditor - Insert image function if want to insert image should add below tag to htmlText of htmlEditor

"<img src=\"" + getClass().getResource(PicName[i])) + "\" width=\"" + target.getImage().getWidth() + "\"height=\"" + target.getImage().getHeight()  + "\">")

but if want to add image in cursor position how to do it?

Community
  • 1
  • 1
FxRi4
  • 1,096
  • 10
  • 15

3 Answers3

4

You can achieve this in the following way

  1. Use the ScenicView to explore the HTMLEditor scenic view
  2. Get WebView inside HTMLEditor
  3. Get WebEngine of that WebView
  4. Run a JavaScript Code to Insert an image to the caret pos using WebEngine

How To Replace HTML using JS Link to Original Post

function insertHtmlAtCursor(html) {
    var range, node;
    if (window.getSelection && window.getSelection().getRangeAt) {
        range = window.getSelection().getRangeAt(0);
        node = range.createContextualFragment(html);
        range.insertNode(node);
    } else if (document.selection && document.selection.createRange) {
        document.selection.createRange().pasteHTML(html);
    }
}

How to Execute JS Code Guide

Node webNode = htmlEditor.lookup(".web-view");
if (webNode instanceof WebView) {
     WebView webView = (WebView) webNode;
     WebEngine engine = webView.getEngine();
     engine.executeScript("alert('helo')"); // add js code here
}
Community
  • 1
  • 1
bhathiya-perera
  • 1,303
  • 14
  • 32
0

text fileds are have a linear datastructure and can have a sequential position and corresponding location in its view. but for html view the displayed object location on screen to location in the text mapping is a bit difficult so thats why the html editor cant determine the cursor position

im working on some other workaround to insert image at given cursor position.

try to insert a dummy text as a placeholder like ###inserImage#### then replace it with the actual image tag <img src="file:\c:\testhtmleditor\sample01.jpg" />..

0

Let's try this piece of code

@FXML private HTMLEditor editor; 
... 
public void insertTextToCurrentFeatureCursorPosition(String text) {
    WebView webView = (WebView) editor.lookup(".web-view");
    WebPage webPage = Accessor.getPageFor(webView.getEngine());
    webPage.executeCommand("insertHTML", text); 
}

Where text is the HTML code which wraps the image

OlivierTerrien
  • 2,451
  • 1
  • 19
  • 31