JavaFX: Css or Class

1

I would like to have some guidance about the JavaFX CSS, the question / problem is this:

I'm trying to create the Hover effect in an ImageView which starts with a opacity of 0.5 ; the question is to create a Style Sheet which calls Efectos.css in which I have the code that makes the effect (I upload a screenshot to be clearer). The fact is that he used the btn_cerrar.getStyleClass().add("imageview"); command but does not create any effect.

Effects.css:

On the other hand, I had thought to leave the CSS and do this effect with code (JavaFX) where I create the following code:

This is created in the FXMLController:

@FXML
private void efectosMouse(MouseEvent event){

    btn_cerrar.setOnMouseEntered((MouseEvent event1) -> {
        btn_cerrar.setStyle("-fx-opacity: 1");
    });

    btn_cerrar.setOnMouseExited((MouseEvent event1) -> {
        btn_cerrar.setStyle("-fx-opacity: 0.5");
    });   
}

The problem is that this same code will be re-used in other elements of ImageView type, to which, I do not want to repeat these lines throughout the code of my program. Is it possible to create this code in a class and go to the class when you need it?

IDE's:

  • Netbeans.
  • SceneBuilder 2.0.

PS: I hope to be clear with my doubts, I thank you in advance for your comments.

    
asked by CamiloJc 22.08.2017 в 05:13
source

1 answer

2

The best way to proceed is the 1st, that is, have a style sheet and apply the styles using class names, ids, etc. That way there is more flexibility in the code, it is easier to maintain or update than if you write the styles directly in each element.

It may not be working because Efectos.css is not loading well. You must verify that it is loaded correctly using the correct path. As the documentation says , style sheets should usually be loaded into the scene. Something like this:

Scene scene = new Scene(new Group(), 500, 400); 
scene.getStylesheets().add("rutacorrecta/Efectos.css");

You can also add the style sheet from Scene Builder, by opening the fxml file to which you want to apply the style, by selecting the root element in the hierarchical view of Scene Builder and then in properties. You can see it explained with images here .

    
answered by 22.08.2017 / 06:37
source