How to add JavaFX stylesheet from another module in Java 9?

I have two JPMS modules:

  • module-a
  • module b

In module-a, I have something like:

public class MyAppplication extends Application {
   ....
   public static void addCss(String path) {
       stage.getScene().getStylesheets().add(path);
   }
}

In module-b, I have a CSS file that I want to add to MyApplication. How to do this in code in module-b? I can’t figure out how to get from another module.

I mean in the -b module:

...
MyApplication.addCss(???);
...

EDIT
In OSGi, I used the following solution in bundle-b(assuming module-a was bundle-a and module-b was bundle-b):

String pathInBundleB = "com/foo/package-in-bundle-b/file.css"
Bundle bundleB = FrameworkUtil.getBundle(this.getClass()).getBundleContext().getBundle();
URL cssFileUrl = bundleB.getEntry(pathInBundleB);
MyApplication.addCss(cssFileUrl.toString());
+4
source share
1 answer

I found a solution using @AlanBateman.

Assuming the css file is in com/foo/some-package/file.css, I use the following code in the -b module:

package com.foo.some-package;

public class SomeClass {

  public void init() {
      MyApplication.addCss(this.getClass().getResource("base.css").toString());
  }
}

Also, in module-information module-b, I have:

opens com.foo.some-package to module-a;
+1

Source: https://habr.com/ru/post/1686244/


All Articles