Css 单击复选框更改代码区文本背景
使用我已经能够创建一个Css 单击复选框更改代码区文本背景,css,checkbox,javafx,highlight,richtextfx,Css,Checkbox,Javafx,Highlight,Richtextfx,使用我已经能够创建一个CodeArea,它接受一个字符串,并突出显示属于string[]KEYWORDS数组的任何单词 我在code区域上方有一个复选框控件,我在控制器代码中将handler()附加到该控件,因此每当单击该复选框时,它都会取消突出显示code区域中所有突出显示的单词。我无法取消突出显示(code区域中的文本)(使文本背景为白色)因为styleClass是在下面显示的StyleSpans方法中定义的。现在一切正常,除了单击复选框,它不会取消突出显示以下文字: FXML: <H
CodeArea
,它接受一个字符串,并突出显示属于string[]KEYWORDS
数组的任何单词
我在code区域
上方有一个复选框控件,我在控制器代码中将handler()
附加到该控件,因此每当单击该复选框时,它都会取消突出显示code区域
中所有突出显示的单词。我无法取消突出显示(code区域
中的文本)(使文本背景为白色)因为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>
@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();
}
.highlightKeyWords{
-fx-font-size:13px;
-fx-background-color: grey;
-fx-background-fill: yellow;
}
这里的-fx背景填充:黄色代码>可以很好地高亮显示,但如何在处理程序中将其更改为白色?使用在代码区定义的“查找颜色”,并使用设置样式更改它
首先,只需将CSS id添加到代码区
:
<CodeArea id="codeBox" fx:id="codeBox" wrapText="true" prefHeight="240.0" prefWidth="339.0">
</CodeArea>
然后在initialize()
方法中
highlightBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
codeBox.setStyle("-keyword-highlight: yellow ;");
} else {
codeBox.setStyle("-keyword-highlight: white ;");
}
});
使用code区域中定义的“查找颜色”,并使用setStyle
对其进行更改
首先,只需将CSS id添加到代码区
:
<CodeArea id="codeBox" fx:id="codeBox" wrapText="true" prefHeight="240.0" prefWidth="339.0">
</CodeArea>
然后在initialize()
方法中
highlightBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
codeBox.setStyle("-keyword-highlight: yellow ;");
} else {
codeBox.setStyle("-keyword-highlight: white ;");
}
});
对詹姆斯,你很棒。我也学到了很多!!导致错误的唯一原因是它应该是highlightBox.selectedProperty()
,并且if
语句缺少其结尾}
,但其他一切都可以正常工作,并且都有意义。再次感谢,是的!詹姆斯,你很棒。我也学到了很多!!导致错误的唯一原因是它应该是highlightBox.selectedProperty()
,并且if
语句缺少其结尾}
,但其他一切都可以正常工作,并且都有意义。再次感谢。