Note: Javafx newbie
Problem:
Many websites and tutorial show one how to set the style of a button using classes or ID's.
After setting the style from an Id, I need to restore the style to the previous state or simply the .button
class.
Some Styles:
I have the following css (extract) used for all my buttons:
.button {
-fx-text-fill: white;
-fx-font-family: "Arial Narrow";
-fx-font-weight: bold;
-fx-background-color: linear-gradient(#61a2b1, #2A5058);
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
}
and an Id for a button that is selected:
#button-selected {
-fx-background-color: linear-gradient(#3f6770, #82c0ce);
}
The code (extract):
//load stylesheet from resources into scene someScene.getStylesheets().add(Main.class.getResource("/gui.css").toExternalForm());
//...
Button b = new Button("some button");
//...
b.getStyleClass().add("button"); <----- adds .button class style to button
some trigger later on calls:
(activeButton, bSelectedButton) -> {
//...
if (!activeButton.equals(bSelectedButton)){
//restore activeButton to ".button" class
bSelectedButton.setId("button-selected");
}
//...
}
As a short summary above: (to put question into a practical application)
A trigger occurs, e.g. mouse click event, calling the lambda.
This checks if the buttons are the "same buttons", if not then:
- clear the previous button's formatting
activeButton
, - and set the new button's
bSelectedButton
style with the#button-selected
id.
Please note:
I have tried:
.button:pressed {
-fx-background-color: linear-gradient(#3f6770, #82c0ce);
}
but it does not change the colours as expected after the click (on the button)
Question:
How do I "unsetId" / "removeId" to restore the previous style of the button,
or simply how do I set the active style on the button from a loaded style sheet?