在JavaFX HTMLeditor中设置本地图像 [英] Set a local image in JavaFX HTMLeditor

查看:545
本文介绍了在JavaFX HTMLeditor中设置本地图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来使用JavaFX HTMLEditor的setHtmlText来添加本地图像。我可以添加远程图像没问题:

  HTMLEditor editor = new HTMLEditor(); 
editor.setHtmlText(< img src = \http://someaddress.com \width = \32 \height = \32 \>);

但不能为本地图片执行此操作

  HTMLEditor editor = new HTMLEditor(); 
editor.setHtmlText(< img src = \categoryButton.fw.png \width = \32 \height = \32 \>);

此按钮与java源位于同一级别。那么为什么这不起作用?

解决方案

使用,如 Javafx文本多字着色

  • Java 8添加 TextFlow 组件,允许您将图像嵌入文本区域。

  • 上述两种技术仅用于数据显示。既不允许编辑带有图像和插入其中的其他节点的多样式文本。目前,JavaFX平台为此功能提供的唯一控件是 HTMLEditor WebView 使用 contenteditable true 或嵌入用JavaScript编写的第三方编辑器



    使用原生JavaFX构建风格化的文本编辑器不依赖于WebView或HTMLEditor的构造,但截至今天我认为还没有准备好广泛使用。


    I'm looking for a way to use JavaFX HTMLEditor's setHtmlText to add a local image. I can add a remote image no problem:

    HTMLEditor editor = new HTMLEditor();  
        editor.setHtmlText("<img src=\"http://someaddress.com\" width=\"32\" height=\"32\" >");
    

    but can't do this for a local image

    HTMLEditor editor = new HTMLEditor();  
        editor.setHtmlText("<img src=\"categoryButton.fw.png\" width=\"32\" height=\"32\" >");
    

    This button is on the same level as the java source. So why won't this work?

    解决方案

    Use getResource to get the location of the local image resource.

    editor.setHtmlText(
      "<img src=\"" + 
         getClass().getResource("categoryButton.fw.png") + 
      "\" width=\"32\" height=\"32\" >"
    );
    

    It works the same way as loading content into WebView as in:

    How to reach css and image files from the html page loaded by javafx.scene.web.WebEngine#loadContent?


    Here is an screenshot:

    And an executable sample:

    import javafx.application.*;
    import javafx.scene.Scene;
    import javafx.scene.web.HTMLEditor;
    import javafx.stage.Stage;
    
    public class HTMLEditorWithImage extends Application {
      @Override public void start(Stage stage) {
        HTMLEditor editor = new HTMLEditor();
        editor.setHtmlText(
          "<img src=\"" + 
             getClass().getResource("Blue-Fish-icon.png") + 
          "\" width=\"32\" height=\"32\" >"
        );
        stage.setScene(new Scene(editor));
        stage.show();
      }
    
      public static void main(String[] args) { launch(args); }
    }
    


    I was just curious if this was the only way of getting an image into a some sort of text area?

    1. With JavaFX 2, you can embed images mixed with text into a FlowPane as described in Javafx Text multi-word colorization.
    2. Java 8 adds a TextFlow component which allows you to embed an image into a text region.

    Both of the above techniques are for data display only. Neither allow for editing of the multi-styled text with images and other nodes inserted in it. For now, the only controls provided by the JavaFX platform for this functionality are the HTMLEditor or a WebView with contenteditable true or an embedded 3rd party editor written in JavaScript.

    There have been some 3rd party efforts to create styled text editors using native JavaFX constructs that don't rely on WebView or HTMLEditor, but as of today I don't think any are ready for widespread use.

    这篇关于在JavaFX HTMLeditor中设置本地图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆