0

使用RichTextFX,我已经能够创建一个CodeArea接受字符串的字符串,并突出显示任何落在String[] KEYWORDS数组中的单词。

CodeArea我在控制器代码中附加了一个复选框控件handler(),因此每当单击该复选框时,它都会取消突出显示CodeArea. 我在取消突出显示(使文本背景为白色)中的文本时遇到问题,CodeArea因为 styleClass 是在StyleSpans下面显示的方法中定义的。现在一切正常,除了当我单击复选框时,它不会取消突出显示单词:

FXML:

<HBox>
   <CheckBox fx:id="highlightBox" mnemonicParsing="false"  prefHeight="25.0" prefWidth="154.0" stylesheets="@styleMain.css" text="Highlight Words" />
</HBox>
<HBox>
   <children>
     <CodeArea fx:id="codeBox" wrapText="true" prefHeight="240.0"    prefWidth="339.0">
     </CodeArea>
   </children>
</HBox>

Java 控制器:

@FXML
CheckBox highlightBox;

@FXML
public CodeArea codeBox;

private static final String[] KEYWORDS = new String[] {
 "one",
 "two",
 "three"
};

private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";

private static final Pattern PATTERN = Pattern.compile("(?<KEYWORD>" + KEYWORD_PATTERN + ")");

public static final String demoText = "one two three four five";

@Override
public void initialize(URL location, ResourceBundle resources) {

  //Highlighting  Words
  codeBox.richChanges().subscribe(change -> {
   codeBox.setStyleSpans(0, computeHighlighting(codeBox.getText()));
  });
  codeBox.replaceText(0, 0, demoText);


  highlightBox.setOnAction(new EventHandler < ActionEvent > () {
   @Override
   public void handle(ActionEvent event) {
    codeBox.setStyle("-fx-background-color: blue"); // this should instead change 'highlightKeyWords' background color to white
   }
  });

  ...
 } //End of initialize


public static StyleSpans < Collection < String >> computeHighlighting(String text) {
 Matcher matcher = PATTERN.matcher(text);
 int lastKwEnd = 0;
 StyleSpansBuilder < Collection < String >> spansBuilder = new StyleSpansBuilder < > ();
 while (matcher.find()) {
  String styleClass =
   matcher.group("KEYWORD") != null ? "highlightKeyWords" :
   null; /* never happens */
  assert styleClass != null;
  spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
  spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());

  lastKwEnd = matcher.end();
 }
 spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
 return spansBuilder.create();
}

在处理程序中,我成功地将背景更改CodeArea为蓝色,但如果我尝试propertyOverview.setStyle("-fx-background-fill: blue;");

它不会改变任何东西,单词保持突出显示。这是因为我应该引用highlightKeyWords在方法中创建的computeHighlighting。我应该使用类似的东西highlightKeyWords.setStyle("-fx-background-fill: white")吗?

CSS:

.highlightKeyWords{
-fx-font-size:13px;
-fx-background-color: grey;
-fx-background-fill: yellow;
}

在这里-fx-background-fill: yellow;可以很好地突出显示,但是如何在处理程序中将其更改为白色?

4

1 回答 1

2

使用在 上定义的“查找颜色”,CodeArea并用 更改它setStyle

首先,只需将 CSS id 添加到CodeArea

 <CodeArea id="codeBox" fx:id="codeBox" wrapText="true" prefHeight="240.0"    prefWidth="339.0">
 </CodeArea>

然后更新CSS如下:

#codeBox {
    -keyword-highlight: yellow ;
}

.highlightKeyWords{
    -fx-font-size:13px;
    -fx-background-color: grey;
    -fx-background-fill: -keyword-highlight;
}

然后用你的initialize()方法做

highlightBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
    if (isNowSelected) {
        codeBox.setStyle("-keyword-highlight: yellow ;");
    } else {
        codeBox.setStyle("-keyword-highlight: white ;");
    }
});
于 2016-01-26T03:12:54.250 回答