我们TextArea在我们的应用程序中使用JavaFX的控件,并尝试将其与Jazzy Spell Check API 集成- 例如,当用户输入不在字典中的错误单词时,这样的单词将被突出显示.
有没有办法在所述控件中突出显示一个单词?我在JavaDocs中看不到任何选项,所以如果有人可以建议一种方法吗?
我想,有可能使用HTMLEditor组件和颜色不同的单词<font face="red=>wrongWord</font>.然而,这会带来拼写检查的许多不同问题,例如html标签和单词计数.
我这里有一个拼写检查器演示,从视觉上看,它正是我想要的(不正确的单词有红色下划线),但我在创建右键单击上下文菜单来应用建议时遇到问题。
我能够获得该Text对象的上下文菜单,但无法使用预测找到要替换的文本在框中的位置。
这是代码:
pom.xml
<dependency>
<groupId>org.fxmisc.richtext</groupId>
<artifactId>richtextfx</artifactId>
<version>0.10.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
<type>jar</type>
</dependency>
Run Code Online (Sandbox Code Playgroud)
拼写检查演示.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.BreakIterator;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.StyleClassedTextArea;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.apache.commons.text.similarity.JaroWinklerDistance;
import org.reactfx.Subscription;
public class SpellCheckingDemo extends Application
{
private static final Set<String> dictionary = new HashSet<String>(); …Run Code Online (Sandbox Code Playgroud)