adding css file to stylesheets in javafx - css

Language: JavaFX
IDE: Netbeans
Problem: I'm trying to add a css file to the stylesheet, but the first line of the following code always generates a NullPointerException:
String css = this.getClass().getResource("double_slider.css").toExternalForm();
scene.getStylesheets().add(css);
I've tried replacing "double_slider.css" with the full path. double_slider.css is currently located in the same package as the class that makes this call. I've also tried all of the variations found at http://introjava.wordpress.com/2012/03/21/linking-a-css-style-sheet-to-javafx-scene-graph/, with no success. Clean and build doesn't help either.
If I place the css file in the build folder where the .class files are dumped, the NullPointerException goes away. But then the css file does not work properly because it references other files in my project.

put your yourname.css file directly under src directory.
scene.getStylesheets().add("yourname.css")
clean and build required

I think your're missing the slashes which causes that the CSS file can't be found. Try to correct your path reference.
For example:
-root
--app/Main.java
--assets/double_slider.css
would be:
String css = this.getClass().getResource("/assets/double_slider.css").toExternalForm();

I had the same problem. I use NetBeans 7.3 and JavaFX 2.2.7, JDK 7.0_21 on Win7.
My solution was to place the .css in the SAME folder as my Java file containing void start(Stage stage). So the Project view looks like this:
ProjectName
Source Packages
pkgWhatever
Main.java
MyCssFile.css
(So the CSS file is IN the package, which I find really weird and contraintuitive. Some doc told me to put it in the root of the project so it could be found at runtime, but that didn't work for me in NB. My app now runs regardless of whether I start the file containing "start(..)" by hitting Ctrl+U or clicking Run on the project's context menu. And it doesn't matter whether I let NB put everything into a JAR or not.)
Here's the code that loads the CSS in the above situation:
URL url = this.getClass().getResource("controlStyle1.css");
if (url == null) {
System.out.println("Resource not found. Aborting.");
System.exit(-1);
}
String css = url.toExternalForm();
scene.getStylesheets().add(css);
Whereas this didn't work:
scene.getStylesheets().add("controlStyle1.css");
Hope this helps.

I had the same problem (in NetBeans 8). I found a solution here : https://blog.idrsolutions.com/2014/04/use-external-css-files-javafx/
My resource file spreadsheet.css was here :
MyApp
-resources
--spreadsheet.css
-source packages
--fr.ccc.myapp.view
---mainView.java
---FXMLMain.fxml
In mainView.java :
File f = new File("resources/spreadsheet.css");
spreadsheet.getStylesheets().add("file:///" + f.getAbsolutePath().replace("\\", "/"));
Hope this helps.

All of the answers are missing one very important part and that's '/' before the name of the css file:
Folder structure:
src
resources
stylesheet.css
Load it like this, notice the starting slash before css file:
scene.getStylesheets().add(getClass().getResource("/stylesheet.css").toExternalForm())

Considering your old code:
String css =this.getClass().getResource("double_slider.css").toExternalForm();
scene.getStylesheets().add(css);
Try changing it to this new code and it will work:
screen.getStylesheets().add(getClass().getResource("double_slider.css").toExternalForm());
When you use getClass(), you don't need to use this keyword.
I hope this work for you. :)

You can add your style.css directly in your .fxml file as an attribute to your root element as this stylesheets="#your_relative_path/style.css".
You can use #../style.css if you want to access the css file that is in your src folder

It's pretty much simple.
this.scene.setUserAgentStylesheet(/resources/blabla.css);
This is what worked for me-

Hmmm, are you on Netbeans? Try to "Clean and Build" the project.

Have you initialized the scene object prior to setting style sheet?
scene = new Scene(myRootLayout, 600, 600); // for example

in your file .java use this
Application.setUserAgentStylesheet(getClass().getResource("application.css")
.toExternalForm());

Folder Structure
In the MedicalLibraryFx.java
scene.getStylesheets().add(getClass().getResource("/css/style.css").toString());
Folder structure when css is in the same directory as controller
scene.getStylesheets().add(getClass().getResource("style.css").toString());

Assuming the file structure is something like this:
-root
--src
---resources
----double_slider.css
---package
----JavaFXFile.java
This is what worked for me:
scene.getStylesheets().add((new File("src/resources/double_slider.css")).toURI().toString());

scene.getStylesheets().add("file:///home/fullpathname/file.css");
or
scene.getStylesheets().add("file:/home/fullpathname/file.css");
but after:
Run / Clean and Build Project
worked for me
NetBeans IDE 8.2 ; Java: 1.8.0_201 ;Linux 16.04

I made a small login example and this is how i link my styleshet.css
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("LoginUI.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(JavaFXLogin.class.getResource("stylesheet.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
You can use this code line to link your css file. but the css file should be in your package.
scene.getStylesheets().add(JavaFXLogin.class.getResource("stylesheet.css").toExternalForm());

In a maven project you must define path for resources in the file.pom in the javafx-plugin section: something like this:
<configuration>
<mainClass>com.personale.ciaomondo.App</mainClass>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>

Try putting '#' the name of the file. It worked for me.
ex:
'#main.css'

Related

How do I load a custom Font in Xamarin using UrhoSharp?

I am trying to follow the example posted by Adam Pedley at
Introduction to UrhoSharp in Xamarin Forms but have run into a problem. In the following code block, he is loading a custom font:
private void CreateText()
{
// Create Text Element
var text = new Text()
{
Value = "Hello World!",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
text.SetColor(Color.Cyan);
text.SetFont(font: ResourceCache.GetFont("Fonts/Anonymous Pro.ttf"), size: 30);
// Add to UI Root
UI.Root.AddChild(text);
}
The problem I have is that I don't have this font, and it isn't included anywhere in any kind of Assets folder or anything in his GitHub repository of his demo project at adamped/UrhoSharp.Demo. In fact, in spite of the allusion to a "Fonts" folder in the function call, no Fonts folder exists in his demo solution in any of the projects.
Trying to run the application results in a mono runtime error " [ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: Could not find resource...".
In addition, there doesn't seem to be any means of contacting the author to ask (which I find doubly frustrating), so I am having to ask the Stack Overflow community: Where and how do I place custom font assets in my solution (his solution) so that a custom font may be used?
I am currently able to use a custom font in a standard Label with the font file being located in the /Assets folder of both my Sample.Android and Sample.UWP projects, but no combination of path or file name I have tried in the call to GetFont(...) works.
After hours of searching, I finally came across a screen shot showing the proper setup for anyone else who may run into this issue;

Handlebars NodeJS/Koa Script/Src Reference

The answer is probably going to make me bang my head on the keyboard, but for the life of me I can't seem to figure out how to reference a template file so that I can load it from a handlebars view.
Let's say the view is 'ROOT/views/intro/home.html'. In it I want to reference an external CSS file called style.css.
Let's say that this external CSS file is located at 'ROOT/assets/css/style.css'.
How would I reference this file in the view in the <link ref=""></link> tag? I have tried everything from
'../../assets/css/style.css' to
'../assets/css/style.css' to
even appending localhost to the front and referencing
'localhost/assets/css/style.css'.
Thank you in advance!
Figured it out! For anyone else having this problem, you have to declare the static path for public files in the middleware, using something like koa-static and opening up that folder for access like:
_app.use(serve(__dirname + '/public/'));
Hope this saves someone a lot of time! It definitely was a trip trying to figure this one out.

How to retrieve the applications icon at runtime that was set with .rc file on Windows or .icns file on Mac?

There's a method QApplication::windowIcon(), but it only works if you've set the icon with setWindowIcon. How to get the application icon that was set by specifying .rc or .icns file?
If I understand your problem correctly try this:
QIcon programIcon() {
QFileInfo fileInfo(qApp->arguments().at(0));
return QFileIconProvider().icon(fileInfo);
}
See QFileIconProvider.
If I misunderstood you then this should give you a clue, how to solve your problem.

How to give path to Styles folder, one directory up, asp.net

i am using Obout combobox, its styles folder is one directory up, when i give its path in FolderStyle, its detected by Visual Studio and the combobox with new style appears, but when i run it on browser, it gives the error
Cannot use a leading .. to exit above the top directory.
Please let me know how to resolve this issue.
For referncing style sheet or javascript files you can do something like this
../Styles to go one step back and find Styles folder
../../Styles to go two step back and find Styles folder
The best solution would be to use the ~ character:
< obout:ComboBox runat="server" ID="ComboBox1" FolderStyle="~/ComboBox/styles/black_glass" />

fix file path according to iis

i have a website #local and lots of images in site, but after I deploy site to server images paths are broken(exception given in css file), and I need to fix this as soon as possible.
sample path:
imageurl = #"/Images/sample.gif";
how can i fix this?
thank you.
you can set a appsetting in web.config file with server url like
and get this path on code and add image name with it.
Images (like background-url) in CSS are always referenced relative to the css file.
you need to set in css file like...
background-image: url( '../../Images/image.gif' );
.. this will bring out one folder from current folder hierarchy

Resources