如果行中的文本与用户指定的文本匹配,即用户所说的突出显示包含“警告”一词的所有行等,我需要能够突出显示列表框中的一行... 我四处打听,发现了 this,Alex 在那里向我展示了如何使用样式表。
我实现了上面的解决方案。但现在我想增强它,以便用户可以选择为多个字符串值选择前景色和背景色(指定的第一个值具有最高优先级) 我尝试过使用 CSS 并添加了另一种样式,并根据是否找到文本删除并添加 CSS 样式表以实现如下效果:
荧光笔.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%);
}
用于删除/添加 CSS 的 Java 代码 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("");
}
这会生成类似这样的东西
但问题是我不可能定义样式表中前景色和背景色的所有组合。 有没有一种方法可以根据用户的偏好以编程方式手动添加样式表。 或者还有其他方法可以做到这一点。 这是我为用户设计的屏幕,用于指示应突出显示的内容
最佳答案
您可以通过在 ListView 上使用 cellfactory 轻松完成此操作
listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> stringListView) {
return new ListCell<String>(){
@Override
protected void updateItem(String s, boolean b) {
super.updateItem(s, b); //To change body of overridden methods use File | Settings | File Templates.
if (b) {
setText(null);
setGraphic(null);
}
if (s.contains("warning")){
setStyle(your style here);
setGraphic(your graphics);
setText(your text);
}
if (s.contains("error")){
setStyle(your style here);
setGraphic(your graphics);
setText(your text);
}
}
};
}
});
关于listview - 如何以编程方式用户根据 JavaFX 中的字符串值定义 ListView 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17505015/