How to load css file in JavaFX 2? - css

Wrote my JavaFX classes in com.xyz.abc package (ex: com.xyz.abc.Sample.java)
and wrote my CSS files in `root/css/styles.css.
How do I load this CSS file into Sample.java?

Add it to your scene at the begining of your app
scene.getStylesheets().add("path/to/your/stylesheet.css");

See the CSS reference guide for how paths are resolved:
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.
So your question isn't quite clear, but if "root" is the classpath of your application, you probably want
scene.getStylesheets().add(getClass().getResource("css/styles.css").toExternalForm());
If "root" is actually a directory in the classpath, then replace "css/styles.css" with "root/css/styles.css".

Related

I can't access public folder from css file of my app

I'm trying to access the logo image which is in public folder while my css folder is in src. I'm also using sass and I took into consideration that we need to write the path relatively to css file - not sass.
I tried different ways - with absolute path, with quotes and without - just hoped that maybe something will work.
I found someone's code https://codesandbox.io/s/musing-rosalind-21lsd?file=/src/App.js and played with it - I created same conditions as I have and it worked, but when I go back to my project it doesn't.
I'm using background-image: url('/logo.swg'), and it says Error: Can't resolve '/logo.swg'.
I'm aware of ejecting and webpack configuration changes, also I know that if I change the css and sass folders' path moving it outside of src it may theoretically work (with an absolute path I guess - since React does not allow us to refer to files outside of src directory).
I'm wondering if something changed in React, does anyone know? It works when I write the url as inline style, and it works when I import it in js files.
Thanks in advance.
versions:
react^17.0.1
react-scripts^4.0.1
create-react-app^4.0.2
You can use the following list as quick reference:
/ = Root directory
. = This location
.. = Up a directory
./ = Current directory
../ = Parent of current directory
../../ = Two directories backwards

How to import jar file to scene builder with css file? ( css file does't apply )

I have developing custom button control. Code is very simple. I just create MyButton class extend Button class. And I adding css that in same file. like this
public class PrimaryButton extends Button {
public PrimaryButton(){
getStyleClass().add("primary-button");
getStylesheets().add("primarybutton.css");
}
}
My project structure is this.
I made this project file to jar. And I import this jar to scene builder. But css dose not apply. like this. What is wrong?
I think the problem is with in your CSS file path, and when i changed it,into
getStylesheets().add("sample/primarybutton.css");
it worked fine, and this also worked fine
getStylesheets().add("/sample/primarybutton.css");
NOTE: my app architecture is completely identical to yours
here's the complete sample
public class MyButton extends Button {
public MyButton(){
getStylesheets().add("sample/primarybutton.css");
//you can also use this
//getStylesheets().add("/sample/primarybutton.css");
getStyleClass().add("primary-button");
} }
Note : the "primary-button" is a css class in your "primarybutton.css" file
hope this is useful, and solve your problem
The string you pass to the list of stylesheets is treated as a URL. The documentation states:
The URL is a hierarchical URI of the form [scheme:][//authority][path].
If the URL does not have a [scheme:] component, the URL is considered
to be the [path] component only. Any leading '/' character of the
[path] is ignored and the [path] is treated as a path relative to the
root of the application's classpath.
Since both the class and the stylesheet are in the sample package, the URL of the stylesheet relative to the classpath is sample/primarybutton.css, so you could use
getStylesheets().add("sample/primarybutton.css");
If you want to make use of the fact the the stylesheet is in the same package as the class, and make the code more portable (i.e. not hard-code the package name), you can create a complete URL from the current class, and then convert it to a string:
getStylesheets().add(getClass().getResource("primarybutton.css").toExternalForm());
This works because getClass().getResource(...) creates a URL where the resource name, if it doesn't begin with a leading /, is interpreted as being relative to the current class.
I generally prefer the latter approach, though others may not agree.

Spring MVC referencing absolute urls within JS and CSS files

When you need to reference a resource file from a JSP file within SpringMVC you have to pass an absolute url for the resource, which is traditionally done with either <c:url ... or href="${pageContext.request.contextPath}/css/....
However, how do pass absolute urls within my CSS and JS files? In CSS this can be necessary at times you use a url for a property. In my JS files I may need to make an AJAX call to an absolute URL that I define somewhere. In both of these instances these URLS can change at different times, but Googling has not pointed me to the best way to handle cases like these.
I wouldn't be above adding a new maven plugin or some other JS or CSS compiler to achieve this.
CSS URLs don't need to be absolute, because the URLs are not resolved relative to the path of the current page, but relative to the path of the CSS file itself.
For JS, you simply have to define, for example, the base URL in a global variable, from your base JSP template, and reuse that base URL from your JS files:
In your JSP:
<script>var BASE_URL = "<c:url value='/' />";</script>
<script src="someFile.js"></script>
In your JS file:
$('#foo').load(BASE_URL + 'some/path.html');

Orchard default path for CSS

Is there a way to point the default CSS path to a different directory in Orchard? I would like to locate my CSS in the content directory.
Orchard uses the ~/Scripts folder to locate a stylesheet file in a given module or theme, if you provide a relative path. You can change that behavior, but that'd be a hard thing to do and would possibly introduce many errors.
I guess the best way to do would be to provide full path relative to the root, eg:
#{ Style.Include("~/Themes/MyThemeName/MyCustomFolder/Site.css"); }
If you provide a full path, Orchard will not look for your stylesheet in /Styles folder. Knowing that, you can create an extension method for ResourceRegister, providing your own implementation of Include method, that would append the custom path to the provided one.
Eg:
public static ResourceExtensions
{
public static RequireSettings CustomInclude(this ResourceRegister register, string path)
{
// Construct your own path, by eg.
// appending custom base path to given path
var myPath = ....;
return register.Include(myPath)
}
}
And now you can use Style.CustomInclude(...) instead of Style.Include(...) inside your shapes and get your custom stylesheet path.
What you probably want to do is create a theme. The way to do it is described in this page of the orchard documentation.

Extending parent routing when extending bundles

In Symfony2 (2.0.3) I have a BetaBundle that is set as the parent of a AlphaBundle. Is it possible to override some routes while still keeping the parent originals routing definition ?
I have tried importing the parent routing.yml inside the child routing.yml file but it naturally result in a circular reference exception.
Is there any standard way to achieve this using yml and files named routing.yml in the same relative path ?
When you override your AlphaBundle anything that uses the #AlphaBundle shortcut will look in #BetaBundle first. The only way I've found to solve this kind of problem is to have the extending bundle (BetaBundle) quit using the # shortcut, and include your AlphaBundle's route using it's path.
As an alternative you could try renaming your BetaBundle's route file so that it doesn't override AlphaBundle's file, and then configure your app/config/routing.yml to include your BetaBundle's renamed routing file.

Resources