Hi
how to paint multiple icons (ie., say icons between text) in jlabel using graphics? plz do assist me in this effort
Thanks
Hi
how to paint multiple icons (ie., say icons between text) in jlabel using graphics? plz do assist me in this effort
Thanks
One option I would use is to have a sequence of JLabels. Ones with icon and ones with text only.
The other option would be to leverage the mini HTML support of the JLabel: Have
<html><img src='theimage.png'>The text<img src='theimage2.png'>
as the text of the JLabel. This approach works for text formatting but I'm not sure if the image tags work there too.
Or you did override the JLabel.paint() to do custom rendering btw?
Then I would use the following approach:
List<Object> textAndImage = new ArrayList<Object>() {{
add("This ");
add(new ImageIcon("image1.png"));
add(" is ");
add(new ImageIcon("image2.png"));
add(" an ");
add(" imaged text sample ");
}};
FontMetrics fm = g.getFontMetrics();
int x = 0;
for (Object o : textAndImage) {
if (o instanceof String) {
g.drawString((String)o, x, fm.getHeight());
x += fm.stringWidth((String)o);
} else
if (o instanceof ImageIcon) {
((ImageIcon)o).paintIcon(null, g, x, 0);
x += ((ImageIcon)o).getIconWidth();
}
}
Of course this is not a fully fledged solution but might give you some hints how to proceed.
One way is to use a custom border that paints an icon (and text if you want), then you can nest indefinitely.
Here's one such border:
/**
* Show a leading or trailing icon in border.
*/
public static class IconBorder implements Border
{
/**
* @param icon - icon to paint for this border
* @param top outer insets for this border
* @param left
* @param bottom
* @param right
*/
public IconBorder(Icon icon, int top, int left, int bottom, int right)
{
setIcon(icon);
top_ = top;
left_ = left;
bottom_ = bottom;
right_ = right;
rect_ = new Rectangle(0, 0, icon_.getIconWidth(), icon_.getIconHeight());
}
public Insets getBorderInsets(Component c)
{
if( icon_ == null )
return new Insets(0, 0, 0, 0);
return isTrailing_ ? new Insets(top_, left_, bottom_, icon_.getIconWidth() + right_) :
new Insets(top_, icon_.getIconWidth() + left_, bottom_, right_);
}
public boolean isBorderOpaque()
{
return false;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
{
if( icon_ != null )
{
if( isTrailing_ )
x = width - icon_.getIconWidth() + 4;
else
x += left_ - 1;
icon_.paintIcon(c, g, x, y + top_);
rect_.x = x;
rect_.y = y;
}
}
public void setIcon(Icon icon)
{
if( icon_ != icon )
icon_ = icon;
}
public Icon getIcon()
{
return icon_;
}
public void setPosition(boolean isTrailing)
{
isTrailing_ = isTrailing;
}
public Rectangle getIconRect()
{
return rect_;
}
private final int top_;
private final int left_;
private final int bottom_;
private final int right_;
private final Rectangle rect_;
private Icon icon_;
private boolean isTrailing_ = true;
}
I use this to add a search icon to a JTextField (a la browser search box). getIconRect
can be used to check for mouse hover. For example I change cursor to HAND_CURSOR
when mouse is over the search icon.