Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Loading Stylesheets in JavaFX with Java 9 Module System

 
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to load a stylesheet that is saved as a resource in my module, but receive the following warning when running the application under JDK9 using the module system:

When running under JDK 8, this was loaded fine with the following directory structure:



It is my understanding that for named modules in Java 9, I should be able to use either the Class or ClassLoader to load a resource and that doing so looks for the resource in module/package.name, e.g. chapter8-styling-nodes/com/jdojo/resources/css/buttonstyles.css, and I've updated the module structure to reflect this. However, if I attempt to load the stylesheet this way, I get various exceptions related to reflection. I can load the resource and read it from an InputStream:



I obviously can't add this to the scene's stylesheets as it's not a URL. If I try to load the stylesheet as a URL and convert it to a string, the below excerpt yields the following result:



Using the same method as for JDK8 results in the resource not being found. Adjusting the path for JDK9 seems to have similar effects. How should I go about adding a stylesheet to a scene in a named module in Java9?
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to this presentation the correct way to load resources is using Class.getResource(). As it transpires, it's right. I'm not sure what I did yesterday that caused this not to work, probably not sleeping! However, the following fixes the problem I was having:

 
reply
    Bookmark Topic Watch Topic
  • New Topic