JavaFX ListView - CellFactory 样式
JavaFX ListView - CellFactory styling
我正在使用带有 CellFactory 的 ListView,为每个列表条目生成 Labels
。 选中时,无论我为list-view
或[=15=设置的CSS
样式如何,Labels
的文本都会变成白色].
如何控制 CellFactory
又名 ListCell
使用的 Labels
的颜色/样式行为?
items.addAll(Arrays.asList("Test 1","Test 2","Test 3"));
lstPreview.setItems(items);
lstPreview.setCellFactory(i -> new ListCell<String> () {
@Override
protected void updateItem (String item,boolean empty){
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setGraphic(new Label(item));
}
}
});
当列表单元格被选中时,您可以将 css 应用于 label
。
对于选定的列表单元格,css 是
.list-cell:filled:selected {
...
}
对于选定的列表单元格,它有一个标签,我们可以写
.list-cell:filled:selected .label {
...
}
要为标签着色,您可以使用 -fx-text-fill
.list-cell:filled:selected .label {
-fx-text-fill: red;
}
如果内容是Label以外的任何其他节点,您可以使用相同的方法。
输出结果如下
编辑
如果您只想覆盖默认的白色,您可以覆盖选定的 .label
。
.list-cell:filled:selected .label {
-fx-text-fill: black ;
}
我正在使用带有 CellFactory 的 ListView,为每个列表条目生成 Labels
。 选中时,无论我为list-view
或[=15=设置的CSS
样式如何,Labels
的文本都会变成白色].
如何控制 CellFactory
又名 ListCell
使用的 Labels
的颜色/样式行为?
items.addAll(Arrays.asList("Test 1","Test 2","Test 3"));
lstPreview.setItems(items);
lstPreview.setCellFactory(i -> new ListCell<String> () {
@Override
protected void updateItem (String item,boolean empty){
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setGraphic(new Label(item));
}
}
});
当列表单元格被选中时,您可以将 css 应用于 label
。
对于选定的列表单元格,css 是
.list-cell:filled:selected {
...
}
对于选定的列表单元格,它有一个标签,我们可以写
.list-cell:filled:selected .label {
...
}
要为标签着色,您可以使用 -fx-text-fill
.list-cell:filled:selected .label {
-fx-text-fill: red;
}
如果内容是Label以外的任何其他节点,您可以使用相同的方法。
输出结果如下
编辑
如果您只想覆盖默认的白色,您可以覆盖选定的 .label
。
.list-cell:filled:selected .label {
-fx-text-fill: black ;
}