在 JavaFX TextArea 中突出显示字符串 [英] Highlighting Strings in JavaFX TextArea

查看:34
本文介绍了在 JavaFX TextArea 中突出显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在应用程序中使用 JavaFX 的 TextArea 控件,并尝试将其与 Jazzy 拼写检查 API - 例如,当用户输入字典中没有的错误单词时,该单词将被突出显示.

We are using JavaFX's TextArea control in our application, and trying to integrate it with Jazzy Spell Check API - as in, when a user enters a wrong word that is not in the dictionary, such word would be highlighted.

有没有办法突出显示所述控件中的单词?我在 JavaDocs 中没有看到任何选项,所以如果有人可以建议一种方法吗?

Is there a way to highlight a word in said control? I've seen no options for that in the JavaDocs, so if someone could suggest an approach?

我想,有可能使用 HTMLEditor 组件并使用 <font face="red=>wrongWord</font> 为单词着色>. 然而,这给拼写检查带来了很多不同的问题,例如 html 标签和单词计数.

It could be possible, I guess, to use the HTMLEditor component and color the words diferently with <font face="red=>wrongWord</font>. This, however, brings a whole lot of different problems with the spell checking, such as the html tags and words count.

推荐答案

JavaFX TextArea 控件(从 2.0.2 开始)不支持混合文本样式(字体等)的富文本编辑.

The JavaFX TextArea control (as of 2.0.2) does not support rich text editing where text styles (fonts, etc) are mixed.

您可以通过操作 TextArea 的 selectRange 来突出显示 TextArea 中连续的字符串,如下例所示:

You can highlight contiguous strings of characters in the TextArea by manipulating the TextArea's selectRange, as in the following example:

public class TextHighlight extends Application {
  public static void main(String[] args) { Application.launch(args); }
  @Override public void start(Stage stage) {
    final TextArea text = new TextArea("Here is some textz to highlight");
    text.setStyle("-fx-highlight-fill: lightgray; -fx-highlight-text-fill: firebrick; -fx-font-size: 20px;");
    text.setEditable(false);
    text.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent t) { t.consume(); }
    });

    stage.setScene(new Scene(text));
    stage.show();

    Platform.runLater(new Runnable() {
      @Override public void run() { text.selectRange(13, 18); }
    });
  }
}

您可以使用上述代码作为基础,在进行拼写检查时将 TextArea 切换为只读模式.实施提示以依次查找和修复每个单词,直到拼写检查完成.在单独的对话框或面板中执行提示.Jazzy 演示似乎以这种方式工作 http://jazzy.sourceforge.net/demo.html,所以它应该是将其 Swing UI 转换为 JavaFX 相当容易.

You could use the above code as a basis to switch the TextArea to read-only mode while spell checking is happening. Implement prompting to find and fix each word in turn until the spell check is complete. Perform the prompting in a separate dialog or panel. The Jazzy demo seems to work this way http://jazzy.sourceforge.net/demo.html, so it should be fairly easy to convert its Swing UI to JavaFX.

或者,您可以使用 JavaFX WebView 控件来包装许多基于 javascript/html 的拼写检查器(例如 http://www.javascriptspellcheck.com/) 使用类似于此处演示的技术:http://jewelsea.wordpress.com/2011/12/11/codemirror-based-code-editor-for-javafx/.

Alternately, you could use a JavaFX WebView control to wrap any of the many javascript/html based spell checkers (e.g. http://www.javascriptspellcheck.com/) using a technique similar to what is demonstrated here: http://jewelsea.wordpress.com/2011/12/11/codemirror-based-code-editor-for-javafx/.

这篇关于在 JavaFX TextArea 中突出显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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