Ood
Ood

Reputation: 1795

JLabel - Change position of Title

In a (Java Swing) JLabel the title is usually placed behind the JLabel's icon. Is it possible to move the text (e.g. before or under the icon image)?

I hope my question makes sense to you, thanks in advance!

Upvotes: 0

Views: 1558

Answers (1)

Braj
Braj

Reputation: 46851

Note that labels are not opaque by default. If you need to paint the label's background, it is recommended that you turn its opacity property to "true". The following code snippet shows how to do this.

label.setOpaque(true);

sample code to set the position of the text, relative to the icon:

ImageIcon icon = createImageIcon("images/middle.gif");
. . .
label1 = new JLabel("Image and Text",
                    icon,
                    JLabel.CENTER);
//Set the position of the text, relative to the icon:
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.CENTER);

See Swing Tutorial on How to Use Labels for detail description.

Upvotes: 2

Related Questions