【问题标题】:Change the CodeArea text background on checkbox click单击复选框更改 CodeArea 文本背景
【发布时间】:2016-01-26 02:02:05
【问题描述】:

使用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;");

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

CSS:

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

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

【问题讨论】:

    标签: css checkbox javafx highlight richtextfx


    【解决方案1】:

    使用在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 ;");
        }
    });
    

    【讨论】:

    • 是的!詹姆斯,你很棒。我也学到了很多!!唯一导致错误的是它应该是highlightBox.selectedProperty(),而if 语句缺少结束},但其他一切都有效,并且有意义。再次感谢。
    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多