I tried to add external style sheet to my JavaFX
application.
Example:
scene.getStylesheets().add("http://localhost/css/style.css");
But it didn't work, so is there any way to load the css file from external resource , and add its contents to the scene css classes ? or add its css file to scene style sheet files?
I think it is reasonable that JavaFX users would think it possible to do this. The documentation does specify that the add method takes a URL as a string.
Beginning with JavaFX 2.1, the Parent class has a stylesheets property, allowing style sheets to be set on a container. This allows for one branch of of the scene graph to have a distinct set of styles. Any instance of Parent can have a style sheets. A child will take its styles from its own inline styles, the style sheets of all its ancestors, and any style sheets from the Scene.
A style sheet URL may be an absolute URL or a relative URL. If a relative URL is given, it is resolved against the base URL of the ClassLoader of the concrete Application class. If, for example, there is a main class com.wicked.cool.ui.Main that extends Application, the relative URL "com/wicked/cool/resources/styles.css" would resolve correctly. The relative URL "../resources/styles.css" would not since the path ".." relative to the root is not a valid path. It is often easier to use the ClassLoader of some class to find the resource. For example, if the "styles.css" file resides in the same package as Main, the following code will give the correct URL: com.wicked.cool.ui.Main.class.getResource("styles.css").toExternalForm()
Note that, beginning with JavaFX 2.1, a URL consisting of only an absolute path (having no scheme or authority) is resolved relative to the base URL of ClassLoader of the class that extends Application. In other words, "/com/wicked/cool/resources/styles.css" is treated as "com/wicked/cool/resources/styles.css". This is consistent with FXML. The implementation allows designers to style an application by using style sheets to override property values set from code. This has implications for the cascade; particularly, when does a style from a style sheet override a value set from code? The JavaFX CSS implementation applies the following order of precedence; a style from a user agent style sheet has lower priority than a value set from code, which has lower priority than a Scene or Parent style sheet. Inline styles have highest precedence. Style sheets from a Parent instance are considered to be more specific than those styles from Scene style sheets.
The documentation talks about absolute and relative URLs in relationship to the class loader though. So, if using a URL that points to some resource on the internet does not work I think the best thing you could do is file a bug report. Either a URL pointing to a resource on the internet should work, or the documentation should explicitly specify that only local resources (local to the classloader) are valid.