JavaFX style sheets in OSGi packages

I've put "styles. CSS" in the root directory of my package, and now I'm trying to figure out how to link it in the code The problem is @ getStylesheets(). add(_) Accept a string instead of a URL, so all the methods I know failed:

Take 1:

scene.getStylesheets().add("styles.css");

Nov 15,2013 2:04:47 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "styles.css" not found.

Take 2:

scene.getStylesheets().add(this.getClass().getResource("styles.css").toExternalForm());

NullPointerException

Take 3:

scene.getStylesheets().add(this.getClass().getClassLoader().getResource("styles.css").toExternalForm());

Nov 15,2013 2:27:31 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not load stylesheet: bundle://5.0:1/styles.css

Take 4:

scene.getStylesheets().add(myBundle.getEntry("styles.css").toExternalForm());

Nov 15,2013 1:31:35 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not load stylesheet: bundle://5.0:0/styles.css

I use felix-4.2 1,Java-8(build 1.8.0-ea-b115),JavaFX-8(8.0.0-ea-b115).

Solution

I recently encountered the same error / problem as the original poster for this problem, and I managed to fix it without making any changes to CSS / fxml or extracting it to a temporary file I post here because I haven't found any good answers on the Internet:

In my case, the settings are:

Bundle0:

> someview. Fxml with relative path reference to CSS file > somestyle CSS with relative path reference to Image > someimage png > Resourceloader. Java (return URL objects from CSS and fxml files)

Bundle1:

>Use the fxml loader in bundle 0 to load the basic JavaFX file of fxml

Question:

Although I set the class loader on the fxml loader, fxml seems to ignore all CSS and CSS referenced images It works only when I use the absolute file path of CSS and the image referenced in CSS This is not what I want

Solution:

There are 2 errors / design problems in JavaFX. It is forbidden to use this setting with OSGi

>Load CSS from fxml > reference image from CSS

To solve the first problem, you must set the system property binary CSS is set to false to disable binary CSS files in JavaFX There is an error in JavaFX (may it have been fixed?), Even if you say CSS, JavaFX tries to be smart and tries to find BSS files It will flip because the OSGi resource URL has a 'bundle: / /' schema, which does not need a schema Solution:

> -Dbinary. css = false

To solve the second problem, you must set the thread context classloader to the classloader of the package where the CSS image is located, and then restore the thread context classloader The trick is to do this every time JavaFX interprets CSS, which is not always when you expect

ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        try {
            final ClassLoader resourcesClassLoader = Resourceloader.class.getClassLoader();
        //needed for css
        Thread.currentThread().setContextClassLoader(resourcesClassLoader);
        this.primaryStage.show();
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

Unfortunately, this is the best solution I can think of Although almost every enterprise application uses these functions in some form, JavaFX does not consider class loaders or dependency injection

The above is all the JavaFX style sheets in the OSGi package collected and sorted by the programming home for you. I hope this article can help you solve the program development problems encountered by the JavaFX style sheets in the OSGi package.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>