Image class and paths relative to classpath
martin.sladecek at oracle.com
Fri Apr 27 06:31:40 PDT 2012
I guess I can add openjfx-dev again.
On 04/27/2012 03:18 PM, Greg Brown wrote:
>> Currently, Image constructor takes paths as filesystem paths, so /foo/bar.png looks for /foo/bar.png on Linux (don't know how this is resolved on Windows). This is why we need special classpath: URI scheme.
> So, if "/foo/bar.png" is considered an absolute file system path, would it make sense for "bar.png" to be considered relative to the current working directory?
Yes, this is the way it now works. There is currently now way how to
pass any classpath URL to Image constructor directly.
>> And as both fxml and css use Image constructors, they need to be aware of this.
> FXMLLoader actually does not have any special information about Images. It simply converts any attribute value that begins with "@" to a URL. If the first character after "@" is "/", then the value is treated as an absolute classpath URL. Otherwise, the value is converted to a URL by appending the path to the URL that was used to load the document.
> It just occurred to me that trying to load the image relative to the calling code could be difficult - how do we know which class it should be relative to? The call stack could contain any number of calling classes - how would we know which one is the "right" one? I don't think there's any reliable way to know.
> So I think a "classpath:" URL scheme makes sense, though I'm not sure how useful it would be in practice. It wouldn't be necessary in FXML (not sure about CSS), so the only place you'd need to use it is in code:
> Image image = new Image("classpath:///foo/bar.png");
> However, you can already do this as follows:
> Image image = new Image(getClass().getResource("foo/bar.png");
> and the latter does not require a custom URL stream handler, whereas a "classpath" URL would.
I agree that relative classpath URL is very tricky in terms of context,
so maybe it would be best to support just absolute classpath URLs.
This means CSS & FXML won't need to handle it in any way.
More information about the openjfx-dev