What is wrong with my syntax calling a stylesheet (css) from an FXML file?

雨燕双飞 提交于 2019-11-29 14:46:05

Let me consider your project structure as follows

MyApp
  |
  |_ src
      |
      |_controllers (Controllers)
      |_view (FXML)
      |_style (css)
          |_myStyle.css

You can specific the relative path in the FXML using

<stylesheets>
   <URL value="@../style/myStyle.css" />
</stylesheets>

In your controller, you can add it as follows

layout.getStylesheets().add(getClass().
         getResource("/style/myStyle.css")).toExternalForm();

This is how it works for me:

<?import java.net.*?>

<fx:root type="javafx.scene.layout.BorderPane" ... xmlns:fx="http://javafx.com/fxml">
  <stylesheets>
    <URL value="@myStyle.css" />
  </stylesheets>
</fx:root>

The css file is in the same package (folder) as the FXML. Also my root happens to be <fx:root>, I expect it to work the same for your <AnchorPane>.

Check out the docs, search for "Location Resolution" for details.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!