mkpaz / atlantafx

Modern JavaFX CSS theme collection with additional controls.
https://mkpaz.github.io/atlantafx
MIT License
791 stars 64 forks source link

How to set background color for selected but not focused table row? #97

Closed PavelTurk closed 3 weeks ago

PavelTurk commented 4 months ago

I need to set background color for selected but not focused table row. This is my java code:

public class JavaFxTest10 extends Application {

    private static record Student (int id, int mark) {};

    private TableView<Student> students = new TableView<>(FXCollections.observableList(
            List.of(new Student(1, 3), new Student(2, 4), new Student(3, 5))));

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Application.setUserAgentStylesheet(new Dracula().getUserAgentStylesheet());
        var idColumn = new TableColumn<Student, Integer>("Id");
        idColumn.setCellValueFactory((data) ->  new ReadOnlyObjectWrapper<>(data.getValue().id()));
        var markColumn = new TableColumn<Student, Integer>("Mark");
        markColumn.setCellValueFactory((data) ->  new ReadOnlyObjectWrapper<>(data.getValue().mark()));
        students.getColumns().addAll(idColumn, markColumn);

        VBox root = new VBox(new TextField(), students);
        var scene = new Scene(root, 400, 300);
        var css= this.getClass().getResource("test10.css").toExternalForm();

        scene.getStylesheets().addAll(css);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

And CSS:

.table-view {
    -fx-selection-bar-non-focused: yellow;
}

This solution works if I use pure JavaFX:

Peek 2024-05-09 18-21

However, with atltantafx it doesn't work - once selected row has always the same color - it doesn't matter if it is focused or not:

Peek 2024-05-09 18-22

Could anyone say how to do it with atlantafx?

PavelTurk commented 3 months ago

No one can help? I've checked Sample - there also if table row is selected it is not possible to deselect it when table looses focus. At least I didn't find how to do it.

mkpaz commented 3 weeks ago
.table-view {
    -color-cell-bg-selected: red;
    -color-cell-fg-selected: yellow;
    -color-cell-bg-selected-focused: green;
    -color-cell-fg-selected-focused: orange;
}

table

You can find all color variables in the doc.

PavelTurk commented 2 weeks ago

@mkpaz Sorry for the delayed reply. This issue is really very important for me and I tried, but couldn't find solution. So, thank you very much!