[英]Any alternate to RSyntaxTextArea in Javafx?
我正在為Java使用代碼編輯器,它將在並行計算和分布式計算中使用。 我正在尋找Javafx中RSyntaxTextArea的替代品,我嘗試在Javafx中實現它,但它運行不正常,就像有時一半的文本區域不顯示並且光標滯后於文本區域。
Tab textTab = new Tab("Sample Tab");
RSyntaxTextArea ta= new RSyntaxTextArea();
SwingNode sn = new SwingNode();
String text="";
ta.setText(text);
ta.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
ta.setAntiAliasingEnabled(true);
ta.setCodeFoldingEnabled(true);
RTextScrollPane sp = new RTextScrollPane(ta);
sn.setContent(sp);
textTab.setContent(sn);
我是Javafx的新手,所以我不知道如何解決這些問題。 它也與Javafx的優勢不符。
嘗試使用Tomas Mikula的RichTextFX框架中的CodeArea
(或更StyleClassedTextArea
是StyleClassedTextArea
)組件。
我已經從RichTextFX創建了自己的類,這是代碼
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.concurrent.Task;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.PlainTextChange;
import org.fxmisc.richtext.StyleSpans;
import org.fxmisc.richtext.StyleSpansBuilder;
import org.reactfx.EventStream;
/**
*
* @author Nika
*/
public class SyntaxTextArea {
private static final String[] KEYWORDS = new String[]{
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws",
"transient", "try", "void", "volatile", "while"
};
private static final Pattern KEYWORD_PATTERN = Pattern.compile("\\b(" + String.join("|", KEYWORDS) + ")\\b");
private CodeArea codeArea;
private ExecutorService executor;
public SyntaxTextArea() {
executor = Executors.newSingleThreadExecutor();
codeArea = new CodeArea();
EventStream<PlainTextChange> textChanges = codeArea.plainTextChanges();
textChanges
.successionEnds(Duration.ofMillis(500))
.supplyTask(this::computeHighlightingAsync)
.awaitLatest(textChanges)
.subscribe(this::applyHighlighting);
codeArea.getStylesheets().add(org.fxmisc.richtext.demo.JavaKeywordsAsync.class.getResource("java-keywords.css").toExternalForm());
}
public void setText(String text) {
codeArea.replaceText(0, 0, text);
}
public String getText() {
return codeArea.getText();
}
public void appendText(String text) {
codeArea.appendText(text);
}
public CodeArea getNode(){
return codeArea;
}
public void setStyling(){
}
private Task<StyleSpans<Collection<String>>> computeHighlightingAsync() {
String text = codeArea.getText();
Task<StyleSpans<Collection<String>>> task = new Task<StyleSpans<Collection<String>>>() {
@Override
protected StyleSpans<Collection<String>> call() throws Exception {
return computeHighlighting(text);
}
};
executor.execute(task);
return task;
}
private void applyHighlighting(StyleSpans<Collection<String>> highlighting) {
codeArea.setStyleSpans(0, highlighting);
}
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
Matcher matcher = KEYWORD_PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder
= new StyleSpansBuilder<>();
while (matcher.find()) {
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton("keyword"), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}
}
但是您必須從庫中的RichTextFX添加JAR文件。 但是我不具備關於CSS的知識。 因此,我無法在樣式或主題方面進行改進(我想將Monokai外觀設置為Text Area語法,但是請放心,我有一天會找到方法。或者我確實有人在我之前找到了,請分享。)為您的建議和幫助,尤其是@James_D。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.