Is there any way to change the selection bar text color in a list view? Preferably using CSS. In a TableView, you can use:
-fx-selection-bar-text: white;
But this does not work for a ListView.
UPDATE: The above case happens when using CellFactories to render the cells.
lvRooms.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override public ListCell<String> call(ListView<String> list) {
return new RoomCell();
}
});
In the Cell Factory class, I'd gladly cover the case when the row is selected.
But: It is called just once at the beginning, not every time the selection bar is moved, and therefore the isSelected() method always renders false.
UPDATE 2: This is the RoomCell implementation:
class RoomCell extends ListCell<String> {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
Log.debug("RoomCell called, item: "+item);
final Label lbl = new Label(item); // The room name will be displayed here
lbl.setFont(Font.font("Segoe UI", FontWeight.BOLD, 18));
lbl.setStyle("-fx-text-fill: black");
//lbl.setTextFill(isSelected()?Color.WHITE: Color.BLACK);
if (isSelected()) // This is always false :(
lbl.setStyle("-fx-text-fill: yellow");
if (Rooms.getBoolean(item, "OwnerStatus")) {
lbl.setEffect(new DropShadow(15, Color.BLUEVIOLET));
lbl.setGraphic(new ImageView(
new Image(getClass().getResourceAsStream("images/universal.png"))));
} else {
lbl.setGraphic(new ImageView(
new Image(getClass().getResourceAsStream("images/yin-yang.png"))));
lbl.setEffect(new DropShadow(15, Color.WHITE));
}
setGraphic(lbl);
}
}
}
-fx-selection-bar-text
is a color palette (not css property) defined in a root default CSS selector, which is selector of theScene
. I don't know how are you using it but if you define it (globally since it is scene's selector) like:in your CSS file then all controls' css properties using
-fx-selection-bar-text
will be red.ListView
will be affected as well (see commented out original usages below).However if you want to customize the ListView's style only, override the default properties this way
(Note: only
-fx-text-fill
are overriden. Original values are commented out, where-fx-selection-bar-text
is used):These CSS properties and more are avaliable in built-in caspian.css.
UPDATE: I strongly advice you to read the Cell API. From there
Be warned about the different String items may use the same cell, ending with misleading visual effects/renderings, like
isSelected()
in your code. Additionally in API it saysSo I refactored your code as follows.
All
-fx-text-fill
styles of the cell's text will change according to definitions in CSS file.Now here is a trade-off between cell's text dropshadow effect and its fill colors from CSS file:
-- if you want to use dropshadow effect, you should go like current way, namely creating label, setting its text, give dorpshadow effect to the label and setGraphic(label). However this time you will not prefer to set the text (
setText(item)
) of the cell thus text color styles in CSS file will have no effect.-- On other hand, if you prefer the code that I have refactored, then you should to disable
-fx-background-color
of the cell (which extendsLabeled
) by setting it totransparent
ornull
and set the-fx-effect
to dropshadow in CSS file to be able to apply dropshadow effect to the text directly. Clearing the background of the cell is not the preferred way either IMO. An explanation by the code:Test them to see the difference. That's all.