I have a label that is created by a 3'd Party lib.
Now I want to fill the label with a text (variable length) and icon (variable width), where the text is above the icon and both are left-aligned. But if I do it, I get the text and icon centered to each other. If the text width is longer than the icon width, the icon is centered to the text. In other case the text is centered to the icon. But I want both left-aligned. When I change the horizontal text position from CENTER to LEFT, I get the text left to icon and not above as required.
public class LabelTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(LabelTest::new);
}
public LabelTest() {
JFrame frm = new JFrame();
JLabel label = new JLabel("Hallo World");
label.setIcon(createIcon(100));
label.setVerticalTextPosition(SwingConstants.TOP);
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setHorizontalAlignment(SwingConstants.LEFT);
frm.add(label);
frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frm.setSize(300, 200);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
private Icon createIcon(int width) {
BufferedImage img = new BufferedImage(width, 20, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, width, 20);
g.dispose();
return new ImageIcon(img);
}
}
As a complete hack you can manipulate the Icon to be the same size as the text.
This example uses the Compound Icon.
It also uses a trick of an EmptyBorder to make the component smaller.