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

查看:1547
本文介绍了在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中突出显示连续字符串的字符串。操作TextArea的selectRange,如下例所示:

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的代码编辑器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天全站免登陆