Java JButton Create createIconButton(Class klass, String resource, String fallbackText)

Here you can find the source of createIconButton(Class klass, String resource, String fallbackText)

Description

Creates a JButton with an Icon from the named resource, which should be in the class loader jar file as klass.

License

Open Source License

Parameter

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.

Declaration

public static JButton createIconButton(Class<?> klass, String resource, String fallbackText) 

Method Source Code


//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;
    }
}

Related

  1. createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al)
  2. createCustomButton(Action a)
  3. CreateFlatButton()
  4. createGameRadioButton(String answer, int fontSize)
  5. createHyperlinkButton(String text, String tip, ActionListener action)
  6. createIconButton(ImageIcon icon, int dimension, String tooltipText, java.awt.event.ActionListener action)
  7. createJButton(Container c, String caption, int x, int y, int width, int height, ActionListener al)
  8. createJButton(final String label, final Font font)
  9. createJButton(String label, Font font)