Java Swing Icon mergeComponentAndIcon(JComponent component, Icon icon)

Here you can find the source of mergeComponentAndIcon(JComponent component, Icon icon)

Description

Draw the component at the left of the provided icon

License

Open Source License

Parameter

Parameter Description
component The component to be merged.
icon The Icon to be merged.

Return

The resulting Icon.

Declaration

public static Icon mergeComponentAndIcon(JComponent component, Icon icon) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    /**//from w  w w.  j  a  v  a2 s. c  om
     * Draw the component at the left of the provided icon
     * @param component The component to be merged.
     * @param icon The Icon to be merged.
     * @return The resulting Icon.
     */
    public static Icon mergeComponentAndIcon(JComponent component, Icon icon) {
        Dimension compSize = component.getPreferredSize();
        component.setSize(compSize);
        int compWidth = compSize.width;
        int compHeight = compSize.height;
        int iconY = 0;
        if (icon != null) {
            compWidth += icon.getIconWidth();
            //Set vertical icon alignment to center
            if (compHeight > icon.getIconHeight()) {
                iconY = (int) Math.ceil((compHeight - icon.getIconHeight()) / 2);
            } else {
                compHeight = icon.getIconHeight();
            }
        }
        //Create an icon that is the compound of the checkbox with the row icon
        if (compWidth <= 0 || compHeight <= 0) {
            return null;
        }
        BufferedImage image = new BufferedImage(compWidth, compHeight, BufferedImage.TYPE_INT_ARGB);
        CellRendererPane pane = new CellRendererPane();
        pane.add(component);
        pane.paintComponent(image.createGraphics(), component, pane, component.getBounds());
        if (icon != null) {
            icon.paintIcon(pane, image.getGraphics(), component.getPreferredSize().width, iconY);
        }
        return new ImageIcon(image);
    }
}

Related

  1. loadIcon(String icon)
  2. loadIcon(String resourceName)
  3. loadIcons(String list, String path, Set ignore, ClassLoader loader)
  4. loadLoadingIcon()
  5. makeButtcon(Icon icon, Icon rollover, String tooltip, boolean is_toggle)
  6. mergeIcons(Icon i1, Icon i2, int offsetRechtsOben)
  7. reescala(Icon ic, int maxW, int maxH)
  8. resize(Icon icon, int width, int height)
  9. scaleDown(Icon icon, int maxWidth, int maxHeight)