4

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());
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • What have you tried so far? Update the question with that, please don't simply post the questions without debugging or trying something. – Naman Sep 22 '17 at 19:01
  • 1
    Also please specify, how is this not similar to https://stackoverflow.com/questions/46369101/how-to-get-resourcebundle-from-another-module-in-java-9 ? – Naman Sep 22 '17 at 19:07
  • module-b can access its own resources, so module-b should have a public method which returns the result of an internal call to Class.getResource or Class.getResourceAsStream. – VGR Sep 22 '17 at 21:19
  • @nullpointer That is absolutely different question. That question is about ResourceBunlde. As it found found in order to use ResourceBundle of one module in another, then the first module must provide some JPSM service and the second module must use that service. Knowing it, I still don't see the way to add css to application in another module, do you? – Pavel_K Sep 22 '17 at 21:38
  • @Alan Bateman Could you give any hints? In OSGi we can pass URI that contains address of the bundle. Is there anything similar in JPMS? – Pavel_K Sep 25 '17 at 09:49
  • Can you update the question to indicate if "path" is a URL string with a protocol/scheme (a hierarchical URL) or a resource name. – Alan Bateman Sep 25 '17 at 20:04
  • If I read this correctly then you specify a path to a resource name rather than a URL string. As I understand it, the JavaFX API can accept both but when you specify a resource name then the resource needs to be in a package that is opened unconditionally (`opens com.foo.packageb` in this case). – Alan Bateman Sep 26 '17 at 06:25
  • @AlanBateman My fault. I updated the question to make it clear. – Pavel_K Sep 26 '17 at 07:57
  • @Pavel_K Why would you want to call `addCss()` in module-b? Doesn't it make more sense to have module-a require module-b and then get the StyleSheet from module-b? – ItachiUchiha Sep 26 '17 at 14:28
  • @ItachiUchiha In my case no. Module-a is an application skeleton that doesn't know about any css styles. If any module (like module-b) wants to use this application skeleton then I has to add its styles. – Pavel_K Sep 26 '17 at 14:52

2 Answers2

2

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;
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
0
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());
Thanhpv
  • 64
  • 1
  • 8