0

I have a function in my control class as below, and I have linked the css file with fxml. But the relevant style class doesn't get applied, If I don't load the stylesheet manually.

 private void ValidateRequired(TextField field){
    Tooltip errorm = new Tooltip("This is required");
    errorm.getStyleClass().removeAll();
    Scene scene = field.getScene();
    scene.getStylesheets().add(this.getClass().getResource("../css/sale.css").toExternalForm());
    //If the above line of code is commented out style will not be applied.
    errorm.getStyleClass().add("error");
    Point2D p = field.localToScene(0.0, 0.0);
    errorm.show((Stage)field.getScene().getWindow(),p.getX()
            + field.getScene().getX() + field.getScene().getWindow().getX(), p.getY()
            + field.getScene().getY() + field.getScene().getWindow().getY()+field.getHeight()+2);
}

I have found the exact problem here JavaFX Tooltip customisation. But it doesn't provide any solution to the problem, and is asked about 3 years ago. Here I have linked the the css file in fxml.

 <GridPane prefHeight="1031.0" prefWidth="821.0" stylesheets="@../css/Sale.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="BikeShop.control.SaleControl">
Community
  • 1
  • 1
v1shva
  • 1,261
  • 3
  • 13
  • 28
  • `Tooltip`s are special: they are not `Node`s, but `PopupControl`s. When you specify the stylesheet in the FXML file, the stylesheet is added to the list of stylesheets of the root node of the FXML file (which node will be part of the scenegraph - most likely the root - of a `Scene`). Problem: the `Tooltip` is not the child of this node, therefore it is not applied. When you do `scene.getStylesheets().add()`, you attach the file to the `Scene`, hence it is applied to the `Tooltip`.I fear there is no way to do this from FXML-there the `Scene` is unknown.I hope someone will prove that I'm wrong:) – DVarga Mar 22 '17 at 12:33
  • Thank you for the explanation, I also found one similar to this, if this is the case, I may have to load the stylesheet everytime I initiate tooltip. I hope there's some workaround for this. – v1shva Mar 22 '17 at 12:49

1 Answers1

0

Please write the full path after src package. For example;

<GridPane prefHeight="1031.0" prefWidth="821.0" stylesheets="@/com/test/css/Sale.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="BikeShop.control.SaleControl">
mtrgn
  • 131
  • 2
  • 4