开发者

Using CSS in JavaFX 2.0

I tried to use CSS in JavaFX 2.0. But it doesn't work. I tried to import a CSS开发者_StackOverflow社区 file:

// Import CSS
Group root = new Group();
Scene scene = new Scene(root, 300, 250, Color.LIGHTGREEN);
scene.getStylesheets().add("example.css");

But NetBeans always throws the following exception:

WARNING: com.sun.javafx.css.StyleManager$2 run Resource "null" not found.

I tried to put the CSS file in the project directory, into the src directory and to use a absolute path: "C:\\Users\\janus\\Documents\\NetBeansProjects\\example.css"

Nothing solved the problem


In your src folder add:

src/resources/css/yourStyle.css

then add the following code:

scene.getStylesheets().add("resources/css/yourStyle.css");


This works, if your CSS file is in the same package.

scene.getStylesheets().add(YourClazz.class.getResource("myCssFile.css").toExternalForm());


I had trouble with this too but got it working with something like this:

String cssPath = "/com/yourdomain/resources/style.css";
scene.getStylesheets().addAll(cssPath);

Change the value of cssPath to match the package that contains your css file.


I have found that this whole .css problem can be solved, when using NetBeans, go to the project properties - run - Standalone Application Properties - Working directory and select the path to the directory where the .css file and/or main class is located. I don't know what (windows) is using as default, probably a temp dir.. if so, then there are ofcourse no .css files there and you get the nullpointer exception.


If you want to style your complete application by one CSS you find a solution here: http://www.guigarage.com/2013/03/global-stylesheet-for-your-javafx-application/


One possible reason for this is that you need clean and re-build your project before run.


There are lot of suggestions to putting your css files in various places, but this is what works for me. Any files you add to your project must be in your project home directory that NetBeans creates. For example, if your project name is HiJohn and it resides in /NetBeansProjects/HiJohn you need to place your .css file in the directory HiJohn and you can reference it just using style.css.

For myself, I've created a resource folder in my NetBeans project called resources. This makes it so I can accesss any file in that resource folder using resources/style.css or resources/settings.xml

If you still have questions here is a great resource for getting the basics down:

http://docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm

Good luck!


"file:/"+ new File("styles/intro.css").getAbsoluteFile().getPath()
                    .replaceAll(" ", "%20");


In Eclipse, I found that I could enable the IDE to find the css file as follows:

Create a folder for your css file in your workspace (I had the most reliable success placing it in the src folder of the current project). In the project Properties > Resources > Linked Resources you can create a new variable and use an existing variable to define a nice simple path (e.g., create a Name: RESOURCES and hit Variables, choose PROJECT_LOC, hit Extend, choose/navigate to the folder that you created). What's nice is that Eclipse will immediately display the location you choose, and warn you if you try to reference a location that it can't find.

In your code, your path is now simply "RESOURCES/[fileName.css]".


Use:

root.getStyleSheet().add(
    youclass.class.getResources("YourCSSFileWichIsinYourDefaultPackage").toExternalForm()
);


scene.getStylesheets().add(getClass().getResource("YourCSSFile.css").toString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜