I am trying to make colored toggle buttons to remember their color before they are selected, for example, a black button is selected, it will be gray, and after it is canceled, it will return to black.
When creating buttons, they get the following properties:
cell.setStyle("-fx-border-color: black; -fx-background-color: gray; -fx-base: gray; -fx-border-width: 1");
cell.setOnAction(event -> setPerformAction(cell));
What event:
public void setPerformAction(ToggleButton cell) {
if(cell.isSelected()) {
cell.setStyle("-fx-border-color: red");
}
else{
cell.setStyle("-fx-border-color: black");
}
}
Black and white were applied as follows:
cell.setStyle("-fx-base: white; -fx-background-color: white; -fx-border-width: 1");
But, as you see in the gif below, when the buttons are not selected, they all return to a different color. How can they remember their previous color?
By the way, these buttons are generated dynamically at runtime, so I do not see them in the scene builder, and they do not have css code.

source
share