简体   繁体   中英

How to add to JavaFX Scene 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 CSS file which I want to add to MyApplication . How to do it in code in module-b? I can't understand how to pass path from another module.

I mean in module-b:

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

EDIT
In OSGi I used the following solution in bundle-b (assuming, that 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());

I found the solution with the help of @AlanBateman.

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

package com.foo.some-package;

public class SomeClass {

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

Besides, in module-info of module-b I have:

opens com.foo.some-package to module-a;
package org.apis.style.css;

public class CommonCss {

    public static String getCommonCssStyle(){
       return CommonCss.class.getClassLoader().getResource("common.css").toExternalForm();
    }
}

Export this package to all.

In other module I add this

getStylesheets().add(CommonCss.getCommonCssStyle());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM