Here you can find the source of createIconButton(Class> klass, String resource, String fallbackText)
klass
.
Parameter | Description |
---|---|
klass | Used to specify which class loader is used to retrieve the image resource. Usually <code>yourInstance.getClass()</code> will suffice. |
resource | The location of the resource within the jar file. |
fallbackText | If the image couldn't be loaded, then use this text instead. |
public static JButton createIconButton(Class<?> klass, String resource, String fallbackText)
//package com.java2s; //License from project: Open Source License import java.awt.Image; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; public class Main { /**/* www .j a v a 2 s .com*/ * Creates a JButton with an Icon from the named resource, which should be in the class loader jar file as * <code>klass</code>. * * @param klass * Used to specify which class loader is used to retrieve the image resource. Usually * <code>yourInstance.getClass()</code> will suffice. * @param resource * The location of the resource within the jar file. * @param fallbackText * If the image couldn't be loaded, then use this text instead. * @return */ public static JButton createIconButton(Class<?> klass, String resource, String fallbackText) { JButton result = new JButton(); try { Image image = ImageIO.read(klass.getResource(resource)); result.setIcon(new ImageIcon(image)); } catch (Exception e) { result.setText(fallbackText); } result.setToolTipText(fallbackText); return result; } }