Font icon in swing: font icons not showing in JLabel
I am creating a swing application in java, with buttons of course. I decided to make them through JLabel with icon font for good scalability. I tried Font Awesome with shortcuts and it works great:
Font font = Font.createFont(Font.TRUETYPE_FONT, int.class.getResourceAsStream("/fonts/fontawesome.ttf")).deriveFont(20f);
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout(FlowLayout.TRAILING, 20, 20));
JLabel label1 = new JLabel("\uF144");
label1.setFont(font);
frame.add(label1);
JLabel label2 = new JLabel("\uF1B0");
label2.setFont(font);
frame.add(label2);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
So, I went to the icon font generator (Fontello) and selected the icons I wanted. I added the generated font to my app, changed the code and did a test run ... And I didn't see them: they just didn't show up!
I wrote some test code (I made icons with a red border to see what's going on):
final Font[] font = {Font.createFont(Font.TRUETYPE_FONT, int.class.getResourceAsStream("/fonts/fontello.ttf"))};
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout(FlowLayout.TRAILING, 20, 20));
JLabel size = new JLabel("Size: " + font[0].getSize2D());
frame.add(size);
// I have 12 icons in font
JLabel[] labels = new JLabel[12];
int j = 0;
// There are codes of those icons
for(char i = '\uE800'; i <= '\uE80B'; i++)
{
JLabel label1 = new JLabel("" + i);
label1.setFont(font[0]);
label1.setBorder(new LineBorder(Color.RED, 1));
label1.setForeground(Color.black);
frame.add(label1);
labels[j++] = label1;
}
frame.add(new JButton(new AbstractAction("-1")
{
@Override
public void actionPerformed(ActionEvent e)
{
font[0] = font[0].deriveFont(font[0].getSize2D() - 1);
size.setText("Size: " + font[0].getSize2D());
for(JLabel label: labels)
{
label.setFont(font[0]);
}
}
}));
frame.add(new JButton(new AbstractAction("+1")
{
@Override
public void actionPerformed(ActionEvent e)
{
font[0] = font[0].deriveFont(font[0].getSize2D() + 1);
size.setText("Size: " + font[0].getSize2D());
for(JLabel label: labels)
{
label.setFont(font[0]);
}
}
}));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
And what do I see? Some launches are successful:
(Each run me expands the font size)
Some of them:
As you can see, at a size of 8, I can't tell what happens, starting at a size of 9 characters doesn't get colored at all for some reason.
WTF? Is this a swing bug or font issue?
source to share
So, this is not a solution to the problem I found, but a workaround. I wrote a custom swing component that draws a given glyph from a given font:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.Rectangle2D;
public class FontIconGlyphButton extends JComponent
{
// to avoid wrong glyph codes
public interface FontIconGlyphCode
{
char toCharCode();
}
private final Color color;
private final Color hoverColor;
private final Color disabledColor;
private final Font font;
private GlyphVector vector;
private Rectangle2D size;
private FontIconGlyphCode iconCode;
private boolean hover = false;
public FontIconGlyphButton(Font font, FontIconGlyphCode iconCode, Color color, Color hoverColor, Color disabledColor)
{
this(font, iconCode, color, hoverColor, disabledColor, null);
}
public FontIconGlyphButton(Font font, FontIconGlyphCode iconCode, Color color, Color hoverColor, Color disabledColor, ActionListener listener)
{
this.font = font;
this.color = color;
this.hoverColor = hoverColor;
this.disabledColor = disabledColor;
setIconCode(iconCode);
addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
if(listener != null)
listener.actionPerformed(new ActionEvent(e.getSource(), e.getID(), Action.DEFAULT));
}
@Override
public void mouseEntered(MouseEvent e)
{
hover = true;
repaint();
}
@Override
public void mouseExited(MouseEvent e)
{
hover = false;
repaint();
}
});
}
public void setIconCode(FontIconGlyphCode iconCode)
{
this.iconCode = iconCode;
FontRenderContext context = new FontRenderContext(null, true, true);
vector = font.createGlyphVector(context, String.valueOf(iconCode.toCharCode()));
size = vector.getGlyphMetrics(0).getBounds2D();
repaint();
}
public FontIconGlyphCode getIconCode()
{
return iconCode;
}
@Override
protected void paintComponent(Graphics g1)
{
Graphics2D g = (Graphics2D) g1;
g.setColor(isEnabled() ? (hover ? hoverColor : color) : disabledColor);
g.drawGlyphVector(vector, (float) -size.getX(), (float) -size.getY());
}
@Override
public Dimension getMinimumSize()
{
return size.getBounds().getSize();
}
@Override
public Dimension getPreferredSize()
{
return size.getBounds().getSize();
}
}
source to share