Class ResourceLoader
- Direct Known Subclasses:
SwingFormsResources
public class MyAppResourceLoader extends ResourceLoader {
public static final String LOGO_IMG
= "com/mycompany/myapp/images/logo.png";
public static final String RELEASE_NOTES
= "com/mycompany/myapp/docs/release-notes.txt";
public static BufferedImage getLogo() {
return getImage(LOGO_IMG);
}
public static String getReleaseNotes() {
return getTextResource(RELEASE_NOTES);
}
// And so on for your other resources...
}
You can optionally use setPrefix() to make it easier to load multiple resources from a common path. For example:
ResourceLoader.setPrefix("com/mycompany/myapp/");
BufferedImage logo = ResourceLoader.getImage("images/logo.png");
String helpText = ResourceLoader.getTextResource("docs/help.txt");
// And so on...
Application extensions
Application extensions can use this class to load resources from the application's jar file, but they cannot use this to load resources from their own extension jar files. Extension developers need to implement the loadJarResources() method in their AppExtension subclass and use that class's class loader to load resources from the extension jar. Refer to the javadocs in AppExtension and ExtensionManager for more details, or refer to the swing-extras book for a complete example.
- Since:
- swing-extras 2.7
- Author:
- scorbo2
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanextractResourceToFile(String resourcePath, File outFile) Attempts to extract the given jar resource to the specified output file.static ImageIconLoads the named image resource, scales it to the specified size, and returns it as an ImageIcon.static BufferedImageLoads the given image resource as a BufferedImage and returns it at its natural size.static StringReturns the current resource path prefix.static StringgetTextResource(String resourcePath) Attempts to read the given text resource from the jar and return it as a String.getTextResourceAsLines(String resourcePath) Attempts to read the given text resource from the jar and return it as a List of lines.protected static ImageIconLoads the given image resource as an ImageIcon and returns it at its natural size.protected static ImageIconLoads the named image resource, scales it to the specified size, and returns it as an ImageIcon.protected static BufferedImageLoads and optionally scales the given image resource.static voidYou can optionally set a prefix that will be prepended to all resource paths.
-
Field Details
-
prefix
-
-
Constructor Details
-
ResourceLoader
protected ResourceLoader()
-
-
Method Details
-
setPrefix
You can optionally set a prefix that will be prepended to all resource paths. That way, your calling code can do getImage("icon.png") instead of getImage("my/app/prefix/images/icon.png").The default prefix is "", meaning all calls must specify the full resource path.
- Parameters:
prefix- A resource path prefix common to all your resources, e.g. "/my/app/prefix/"
-
getPrefix
Returns the current resource path prefix. -
getTextResourceAsLines
Attempts to read the given text resource from the jar and return it as a List of lines. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found. If you request a resource that is not text, the result will be garbage. -
getTextResource
Attempts to read the given text resource from the jar and return it as a String. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found. If you request a resource that is not text, the result will be garbage.Note: This method joins lines using the system line separator, regardless of what line endings are present in the original resource. If you'd rather process the lines separately, use getTextResourceAsLines() instead.
-
extractResourceToFile
Attempts to extract the given jar resource to the specified output file. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Returns true if successful, false otherwise. -
getImage
Loads the given image resource as a BufferedImage and returns it at its natural size. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found. -
getIcon
Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found. -
loadIcon
Loads the given image resource as an ImageIcon and returns it at its natural size. Will return null if the resource cannot be found, or if the given URL is null. -
loadIcon
Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon. Will return null if the resource cannot be found, or if the given URL is null. -
loadImage
Loads and optionally scales the given image resource. Pass 0 for size to load at natural size. Will return null if the resource cannot be found, or if the given URL is null.
-