应用CSS样式后,JavaFX ListView多项选择无效

时间:2014-09-18 19:55:06

标签: css listview javafx-8

当我尝试使用CSS设置JavaFX ListView控件的样式时,它会在initialize类的Controller方法中使用下面的代码禁用多重选择模式设置。

listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

有趣的是,当我完全删除CSS时,它运行正常。不知道到底发生了什么。

CSS中的ListView:

.listView .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: white;
}

.listView .list-cell { 
    -fx-text-fill: black;
}

.listView .list-cell:odd { 
    -fx-background-color: #EFEFF0;
}

.listView .list-cell:even { 
    -fx-background-color: #E4E5E6;
}

.listView .list-cell:filled:hover {
    -fx-background-color: linear-gradient(
        #A6A6A6 20%,
        #A0A0A0 40%,
        #9D9D9D 60%,
        #A0A0A0 80%,
        #A3A3A3 100%
        );
    -fx-text-fill: white;
}

.listView {
    -fx-background-radius: 4;
    -fx-border-radius: 4;
    -fx-padding: 2;
}

1 个答案:

答案 0 :(得分:2)

问题出在你的css的第一行,因为缺少listView样式:

错:

.listView .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: white;
}

正确:

.listView .list-cell:filled:selected:focused, 
.listView .list-cell:filled:selected {
    -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%);
    -fx-text-fill: white;
}