Archive for the ‘Java’ Category

OverlayIcon – Displaying overlayed icons in JTree

In companion to the CompoundIcon article, here is a class to allow you to build up icons with overlays that can be used anywhere, but specifically in JTree.

/*
 * (c)2008 mharrison 
 * This class is released under GPLv3
 */
package uk.co.lummie.code;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Vector;
import javax.swing.Icon;
 
public class OverlayedIcon implements Icon {
 
    private Vector _icons = new Vector();
    private int _spaceSize = 2;
 
    public OverlayedIcon(Icon[] icons) {
        for (int i = 0; i < icons.length; i++) {
            _icons.add(icons[i]);
        }
    }
 
    @Override
    public int getIconHeight() {
        int result = 0;
        for (Icon icon : getIcons()) {
            result = Math.max(result, icon.getIconHeight());
        }
        return result;
    }
 
    @Override
    public int getIconWidth() {
        int result = 0;
        for (Icon icon : getIcons()) {
            result = Math.max(result, icon.getIconWidth());
        }
        return result;
    }
 
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        int h = getIconHeight();
        int w = getIconWidth();
 
        for (Icon icon : getIcons()) {
            icon.paintIcon(c, g, x + (w - icon.getIconWidth()) / 2, y + (h - icon.getIconHeight()) / 2);
        }
    }
 
    public int getSpaceSize() {
        return _spaceSize;
    }
 
    public void setSpaceSize(int spaceSize) {
        this._spaceSize = spaceSize;
    }
 
    public void add(Icon icon) {
        _icons.add(icon);
    }
 
    public Vector getIcons() {
        return _icons;
    }
}

CompoundIcon – Displaying more than one icon in JTree

I came across the requirement to display more than one Icon against the nodes in a Jtree. After several hours /days of building custom TreeCellRenderers and TreeCellEditors, inspiration hit. The JLabel can only display one icon, so lets override Icon to display more than one icon, but pretend it’s just a single Icon still.

The following Java class implements a CompoundIcon. Simply create an instance of it passing in the array of Icons you wish to be displayed. Use setSpaceSize to change the space between the icons when the are rendered. Use add, to append new icons if needed after the class has been constructed.

The instance can then be used wherever and Icon is needed.

/*
 * (c)2008 mharrison 
 * This class is released under GPLv3
 */
package uk.co.lummie.code;
 
import java.awt.Component;
import java.awt.Graphics;
import java.util.Vector;
import javax.swing.Icon;
 
public class CompoundIcon implements Icon {
 
    private Vector _icons = new Vector();
    private int _spaceSize = 2;
 
    public CompoundIcon(Icon[] icons) {
        for (int i = 0; i < icons.length; i++) {
            _icons.add(icons[i]);
        }
    }
 
    @Override
    public int getIconHeight() {
        int result = 0;
        for (Icon icon : getIcons()) {
            result = Math.max(result, icon.getIconHeight());
        }
        return result;
    }
 
    @Override
    public int getIconWidth() {
        int result = 0;
        for (Icon icon : getIcons()) {
            result += icon.getIconWidth();
            result += getSpaceSize();
        }
        return result;
    }
 
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        int h = getIconHeight();
        int offset = 0;
 
        for (Icon icon : getIcons()) {
            icon.paintIcon(c, g, x + offset, y + (h - icon.getIconHeight()) / 2);
            offset += icon.getIconWidth();
            offset += getSpaceSize();
        }
    }
 
    public int getSpaceSize() {
        return _spaceSize;
    }
 
    public void setSpaceSize(int spaceSize) {
        this._spaceSize = spaceSize;
    }
 
    public void add(Icon icon) {
        _icons.add(icon);
    }
 
    public Vector getIcons() {
        return _icons;
    }
}