/** * Returns the URL of the resource with the specified name. This implementation first tries to use * the parent class loader to find the resource; if this fails then {@link #findResource(String)} * is called to find the requested resource. * * @param resName the name of the resource to find. * @return the {@code URL} object for the requested resource or {@code null} if the resource can * not be found * @see Class#getResource */ public URL getResource(String resName) { URL resource = parent.getResource(resName); if (resource == null) { resource = findResource(resName); } return resource; }
/** create and format a new button */ private JButton createButton(final String toolTip, final String image, final String title) { JButton newB = null; // first try to get the URL of the image final java.lang.ClassLoader loader = getClass().getClassLoader(); if (loader != null) { final java.net.URL imLoc = loader.getResource(image); if (imLoc != null) { final ImageIcon im = new ImageIcon(imLoc); newB = new JButton(im); } } // just catch any problems if (newB == null) newB = new JButton(title); newB.setMargin(new Insets(0, 0, 0, 0)); newB.setToolTipText(toolTip); return newB; }
/** * Returns the URL of the resource specified by {@code resName}. The mapping between the resource * name and the URL is managed by the class' class loader. * * @param resName the name of the resource. * @return the requested resource's {@code URL} object or {@code null} if the resource can not be * found. * @see ClassLoader */ public URL getResource(String resName) { // Get absolute resource name, but without the leading slash if (resName.startsWith("/")) { resName = resName.substring(1); } else { String pkg = getName(); int dot = pkg.lastIndexOf('.'); if (dot != -1) { pkg = pkg.substring(0, dot).replace('.', '/'); } else { pkg = ""; } resName = pkg + "/" + resName; } // Delegate to proper class loader ClassLoader loader = getClassLoader(); if (loader != null) { return loader.getResource(resName); } else { return ClassLoader.getSystemResource(resName); } }
public URL getResource(String resName) { ClassLoader loader = type.getClassLoader(); if (loader == BootstrapClassLoader.getBootstrapClassLoader()) return ClassLoader.getSystemResource(toResourceName(resName)); else return loader.getResource(toResourceName(resName)); }