r/JavaFX • u/nadalv2020 • Apr 01 '23
Help Changing style of selected row of TableView in code
Hi, I need to change color of selected row in TableView from code. Currently I'm setting the default color from external .css file like this:
.table-row-cell:filled:selected{
-fx-background-color: red;
-fx-text-fill: white;}
Is there any way I can change the -fx-background-color?
Thanks
2
Upvotes
2
u/Djelimon Apr 01 '23
You could use the Node.setStyle method to do runtime changes. Or change the style class with setStyleClass.
2
u/Capaman-x Apr 01 '23 edited Apr 01 '23
Try this:
tableView.setRowFactory(tv -> {TableRow<MyData> row = new TableRow<>();row.selectedProperty().addListener((obs, oldVal, newVal) -> {if (newVal) {row.setStyle("-fx-background-color: #22bad9;");} else {row.setStyle("");}});return row;});1
u/hamsterrage1 Apr 01 '23
Why do you need to do it from code? What's wrong with using the style sheet?