如果行中的文本与用户指定的用户指定的文本相匹配,我需要能够突出显示列表框中的行说突出显示包含单词“警告”或所有行... 我问周围,并遇到this哪里亚历克斯告诉我如何使用样式表。如何以编程方式基于JavaFX中的字符串值为列表视图定义颜色
我实现了上面的解决方案。但现在我想增强它,以便用户可以选择多个字符串值的前后底色应该是什么(第一个指定的是最高优先级) 我玩过CSS并添加了一个样式和基于文本是否被发现或没有我删除,并添加CSS样式表给如下效果:
highlighter.css
/** Highlighting for list-view search result cells */
.list-cell.search-highlight {
-fx-background-color: tomato;
-fx-accent: firebrick;
}
.list-cell:filled:hover.search-highlight {
-fx-background-color: derive(tomato, -20%);
}
.list-cell.search-highlight2 {
-fx-background-color: yellow;
-fx-accent: firebrick;
}
.list-cell:filled:hover.search-highlight2 {
-fx-background-color: derive(yellow, -20%);
}
java代码删除/添加CSS SearchHighlightedTextCell。 java
public class SearchHighlightedTextCell extends ListCell<String> {
private static final String HIGHLIGHT_CLASS = "search-highlight";
private static final String HIGHLIGHT_CLASS2 = "search-highlight2";
private StringProperty searchText = new SimpleStringProperty("");
private StringProperty searchText2 = new SimpleStringProperty("");
SearchHighlightedTextCell(StringProperty searchText, StringProperty searchText2) {
this.searchText = searchText;
this.searchText2 = searchText2;
}
@Override
protected void updateItem(String text, boolean empty) {
super.updateItem(text, empty);
setText(text == null ? "" : text);
updateStyleClass();
searchText.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
updateStyleClass();
}
});
searchText2.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
updateStyleClass();
}
});
}
private void updateStyleClass() {
try {
if (!isEmptyString(searchText.get()) && !isEmptyString(getText()) && getText().toUpperCase().contains(searchText.get().toUpperCase())) {
getStyleClass().remove(HIGHLIGHT_CLASS2);
getStyleClass().add(HIGHLIGHT_CLASS);
} else if (!isEmptyString(searchText2.get()) && !isEmptyString(getText()) && getText().toUpperCase().contains(searchText2.get().toUpperCase())) {
getStyleClass().remove(HIGHLIGHT_CLASS);
getStyleClass().add(HIGHLIGHT_CLASS2);
} else {
getStyleClass().remove(HIGHLIGHT_CLASS2);
getStyleClass().remove(HIGHLIGHT_CLASS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean isEmptyString(String text) {
return text == null || text.equals("");
}
这会产生这样的
但事实是我不能定义样式表WRT前景色和背景色的所有组合。 有没有办法,我可以手动添加样式表编程基于用户的喜好。 或者有另一种方式来做到这一点。 这是我设计,为用户指示屏幕应该怎样强调