ListView styling in JavaFX

烂漫一生 提交于 2020-02-02 03:14:06

问题


I have a listview. I want to change the color of the background and the text fill. I have tried doing the following:

playlistView.setStyle("-fx-background-color: blue; -fx-text-fill: black;");

But, this doesn't work. However, the following is working:

playlistView.setStyle("-fx-font-size: 24px; -fx-font-family: 'SketchFlow Print';");

Can you please tell me how to get the background and text-fill to work? Thank you.


回答1:


You can use the following CSS styles:

.list-view .list-cell:even {
-fx-background-color: blue;
-fx-text-fill: black;
}
.list-view .list-cell:odd {
-fx-background-color: blue;
-fx-text-fill: black;
}
  1. Save the content of this style in a lisStyles.css.
  2. Add the URL for the lisStyles.css style sheet to the scene:

    scene.getStylesheets().add(getClass().getResource("lisStyles.css").toExternalForm());




回答2:


You are styling the ListView, but the background color is determined by styles on the list cells. So style the cells. If you change the value of the looked-up color -fx-control-inner-background you will preserve both the "striping" of alternate rows, and the changing text color so that it contrasts the background. Use

.list-cell {
    -fx-control-inner-background: blue ;
}

in an external style sheet. Note the striping is very subtle (almost invisible) when the background is this dark: you might want to adjust it with

.list-cell {
    -fx-control-inner-background: blue ;
    -fx-control-inner-background-alt: derive(-fx-control-inner-background, 50%);
}

As a quick hack, you can set that looked-up color directly on the list view, and it will propagate to the cells it contains:

playlistView.setStyle("-fx-control-inner-background: blue;");

however, it is better (better separation of code, and more robust) to define it on the cells in an external style sheet.



来源:https://stackoverflow.com/questions/33454279/listview-styling-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!