java ListView 为所选项目设置文本填充样式 - JavaFX 8 MAGIC 还是 BUG?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25832242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ListView styling a selected item text-fill - JavaFX 8 MAGIC or BUG?
提问by MrPisarik
I read this questionand get this code from answer:
我阅读了这个问题并从答案中得到了这个代码:
.list-cell:filled:selected:focused, .list-cell:filled:selected {
/* 3:, 4: */
-fx-background-color: linear-gradient(#333 0%, #777 25%, #aaa 75%, #eee 100%);
-fx-text-fill: white; /* 5 */
}
.list-cell { -fx-text-fill: black; /* 5 */ }
.list-cell:odd { -fx-background-color: white; /* 1 */ }
.list-cell:even { -fx-background-color: #8f8; /* for information */ }
.list-cell:filled:hover {
-fx-background-color: #00f; /* 2 */
-fx-text-fill: white; /* 5 */
}
I would make the selected item with black text-fill (text-color). Therefore I take these classes.list-cell:filled:selected:focused, .list-cell:filled:selected
and change attribute -fx-text-fill
from white
to black
, but nothing happens, but if I change background color to red, then background color have changed (this does not apply to text color, this is just check on right classes)
我会用黑色文本填充(文本颜色)制作所选项目。因此,我使用这些类.list-cell:filled:selected:focused, .list-cell:filled:selected
并将属性-fx-text-fill
从white
to更改为black
,但没有任何反应,但是如果我将背景颜色更改为红色,则背景颜色已更改(这不适用于文本颜色,这只是检查正确的类)
How to change text color of selected item in focused ListView?
如何在焦点列表视图中更改所选项目的文本颜色?
UPD1:
I tried to change text color of list-cell just in this variant:
UPD1:
我尝试仅在此变体中更改列表单元格的文本颜色:
.list-cell{
-fx-text-fill: magenta;
}
and this variant:
和这个变体:
.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:filled{
-fx-text-fill: magenta;
}
but nothing works
但没有任何效果
采纳答案by ItachiUchiha
I tried the following code and it works just fine on Java 8. It changes the text color of the selected cell.
我尝试了以下代码,它在 Java 8 上运行良好。它更改了所选单元格的文本颜色。
ListViewSample.java
ListViewSample.java
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class ListViewSample extends Application {
ListView<String> list = new ListView<String>();
ObservableList<String> data = FXCollections.observableArrayList(
"chocolate", "salmon", "gold", "coral", "darkorchid",
"darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
"blueviolet", "brown");
final Label label = new Label();
@Override
public void start(Stage stage) {
VBox box = new VBox();
Scene scene = new Scene(box, 200, 200);
scene.getStylesheets().add(this.getClass().getResource("list.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("ListViewSample");
box.getChildren().addAll(list, label);
VBox.setVgrow(list, Priority.ALWAYS);
label.setLayoutX(10);
label.setLayoutY(115);
label.setFont(Font.font("Verdana", 20));
list.setItems(data);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
list.css
列表.css
.list-cell:filled:selected {
-fx-text-fill: red;
}
This also works :
这也有效:
.list-cell:filled:selected:focused, .list-cell:filled:selected {
-fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%);
-fx-text-fill: red;
}