Here you can find the source of mergeComponentAndIcon(JComponent component, Icon icon)
Parameter | Description |
---|---|
component | The component to be merged. |
icon | The Icon to be merged. |
public static Icon mergeComponentAndIcon(JComponent component, Icon icon)
//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); } }