2017-03-22 38 views
0

我在我的控件类中有一个函数,如下所示,并且我已将css文件与fxml链接起来。但是相关的样式类没有得到应用,如果我不手动加载样式表。当样式表通过fxml链接时,JavaFX tooltop样式类将不适用

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); 
} 

我在这里找到确切的问题JavaFX Tooltip customisation。但它没有提供任何解决方案,并在3年​​前被问到。这里我已经链接了fxml中的css文件。

<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"> 
+0

'工具提示是特殊的:它们不是'节点',而是'PopupControl's。当您在FXML文件中指定样式表时,样式表被添加到FXML文件的根节点样式表的列表中(该节点将成为场景的一部分 - 很可能是“场景”的根节点)。问题:'Tooltip'不是这个节点的子节点,因此它不适用。当你执行'scene.getStylesheets()。add()'时,你将文件附加到'Scene',因此它被应用到'Tooltip'.I担心没有办法从FXML做到这一点 - 场景'是未知的。我希望有人会证明我错了:) – DVarga

+0

谢谢你的解释,我也发现一个类似于这个,如果是这样的话,我可能不得不加载样式表每次我发起工具提示。我希望这有一些解决方法。 – v1shva

回答

0

请写出src包后的完整路径。例如;

<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"> 
+0

这不起作用。无论如何感谢您的帮助。 – v1shva