My question is whether it is possible to "decorate" a field, such as JTextField or JFormattedTextField, with an icon.
An example would look something like this:
Following an example, I made the following class.
package componentes;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class IconTextField extends JTextField
{
private static final int ICON_SPACING = 4;
private Border mBorder;
private Icon mIcon;
@Override
public void setBorder(Border border)
{
mBorder = border;
if (mIcon == null)
{
super.setBorder(border);
}
else
{
Border margin = BorderFactory.createEmptyBorder(0, mIcon.getIconWidth() + ICON_SPACING, 0, 0);
Border compoud = BorderFactory.createCompoundBorder(border, margin);
super.setBorder(compoud);
}
}
@Override
protected void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
if (mIcon != null)
{
Insets iconInsets = mBorder.getBorderInsets(this);
mIcon.paintIcon(this, graphics, iconInsets.left, iconInsets.top);
}
}
public void setIcon(Icon icon)
{
mIcon = icon;
resetBorder();
}
private void resetBorder()
{
setBorder(mBorder);
}
}
What is missing is to pass a variable with the image path and pass a parameter in instantiation.