Skip to content
Advertisement

JavaFX – Change ListView’s FocusModel

I have a ListView and am currently overriding the SelectionModel to prevent selection of the items, but when you attempt to select an item, it is still showing the outline.

Reading over the API, I found that I can do the same thing but this time by overriding the FocusModel using

    listView.setFocusModel(new SettingsFocusModel<VBox>());

And here is my SettingsFocusModel

public class SettingsFocusModel<T> extends FocusModel<T> {
    @Override
    protected int getItemCount() {
        return 0;
    }

    @Override
    protected T getModelItem(int index) {
        return null;
    }
}

It is not working as intended, though. I can still see the outline as if I did not override the FocusModel. Any help is appreciated!

Advertisement

Answer

For my specific case, I ended up doing the following:

listView.setSelectionModel(new SettingsSelectionModel<VBox>());
listView.getStyleClass().add("settings-view");

SettingsSelectionModel follows the following answer: https://stackoverflow.com/a/46186195/7987450

My css file includes:

    .list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:focused {
    -fx-background-color: null;
}

.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:hover {
    -fx-background-color: null;
}

.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:filled:selected {
    -fx-background-color: null;
}

I chose to set the background color to null each time because this works in both Dark and Light modes.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement