I want to change the style of Node
by changing its style class.
Button button = new Button();
button.getStyleClass().add("class1")
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
button.getStyleClass().add("class2");
}
});
Is it possible to change the style gradually, for making something like a transition?
Yes.
You will need to use setStyle rather than style classes because classes are going to be static things defined in a css. There is no direct support in JavaFX css for animation. You need to perform the animation steps in Java code to modify the css style.
I'd only really recommend this approach when you want to use css to perform the transition because there are no corresponding Java APIs easily available.
To handle the animation, you can use the standard javafx animation Timeline to manipulate properties which the css style property depends on.
For example, bind your style property to string. Then vary the component you want to change (in this case the colorStringProperty) in a timeline.
Here is a sample which flashes a button using css with a gradual color transition of it's base color from gray to red when pressed.
JavaFX2 supports transitions. So you don´t need to make your animation step by step.
Have a look at this: http://docs.oracle.com/javafx/2/animations/basics.htm#CJAJJAGI